0
example = ['1 Hey this is the first (1) level.\n', '2 This is what we call a second level.\n','','3 This is the third (3) level, deal with it.']
example = [i.rstrip() for i in example]

dictHeaders =   [['1 ','One'],
                ['2 ','Two'],
                ['3 ','Three'],
                ['4 ','Four'],
                ['5 ','Five'],
                ['6 ','Six']]

example = [eachLine.replace(old,new,1) if eachLine.startswith(old) for eachLine in article for old, newFront in dictHeaders]

I need it to return...

example = ['One Hey this is the first (1) level.', 'Two This is what we call a second level.','','Three This is the third (3) level, deal with it.']

I have created dictHeaders as a list of lists for a reason to add move values into each key for each instance. For example, if eachLine.startswith(old) then append One to the beginning and possible another string to the end of the line. If I can do the above with a dict, I'd rather go that route.. I'm new to Python.

I thought this is the route to go instead of...

def changeCode(example,dictHeaders):
    for old, newFront in dictHeaders:
        for eachLine in example:
            if eachLine.startswith(old):
            return eachLine.replace(old,newFront,1)

Every time I run the above it only returns the top line, but I need it to return the entire list example modified..

4

2 に答える 2

1

最善の方法は、正規表現を使用して数字を賢く置き換えることです。

import re

examples = [
  '1 Hey this is the first (1) level.\n',
  '2 This is what we call a second level.\n',
  '3 This is the third (3) level, deal with it.',
  '56 This is the third (3) level, deal with it.'
]

class NumberReplacer(object):
  def __init__(self):
    self.ones = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
    self.teens = ['ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen']
    self.tens = ['twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety']

  def convert(self, number):
    if number == 0:
      return 'zero'
    elif number < 10:
      return self.ones[number - 1]
    elif number <= 19:
      return self.teens[number % 10]
    else:
      tens = self.tens[number // 10 - 2]
      ones = number % 10 - 1

      if ones == -1:
        return tens
      else:
        return ' '.join([tens, self.ones[ones]])

  def __call__(self, match):
    number = int(match.group(0))

    return self.convert(number).title()

replacer_regex = re.compile('^\s*(\d+)')
replacer = NumberReplacer()

for example in examples:
  print replacer_regex.sub(replacer, example)
于 2012-09-12T00:33:09.887 に答える
0

あなたは近くにいます。for ループの中から戻ってはいけません。代わりにlist、戻り値として a を作成し、各結果をそれに追加できます

def changeCode(example,dictHeaders):
    retval = []
    for eachLine in example:
        for old, newFront in dictHeaders:
            if eachLine.startswith(old):
                retval.append(eachLine.replace(old,newFront,1))
                break
        else:
            retval.append(eachLine)
    return retval
于 2012-09-12T02:02:06.603 に答える