2012-09-12 19 views
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] 

我需要它返回......replace.if startswith(「某些STRING」),然後返回一個列表,可怕的一個標題

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.'] 

我已經創建dictHeaders爲列表的列表將移動值添加到每個實例的每個鍵中的原因。例如,如果eachLine.startswith(old)然後將One附加到開頭,並可能在該行的末尾添加另一個字符串。如果我能用字典來完成上述操作,我寧願去那條路。我是Python的新手。

我認爲這是去替代路線...

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

我每次運行它上面僅返回前行,但我需要它返回修改整個列表example ..

+0

請問你想始終更換次數是字符串的第一個值? – Onlyjus

+0

當涉及到該列表中的任何內容時,是的。但是我還會遇到其他情況,在整個代碼中需要隨時更換['50':'Fifty']。 – Matthew

回答

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) 
+0

當我有代碼在整個代碼中隨時需要替換['50':'Fifty']的情況時,該如何工作?我應該有一個單獨的字典嗎? – Matthew

+0

沒有。我正在寫點東西。 – Blender

+0

看我的編輯。它比以前更好一點。 – Blender

0

你接近。你不應該從for循環中返回。相反,你可以創建一個list作爲返回值和每個結果追加到它

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 
+0

你對'dictHeaders'中沒有任何東西的列表項目做什麼?我需要能夠返回'retval'中的那些。 – Matthew

+0

@Matthew,在這種情況下,你可以使用顯式的'break'並在'for'循環中添加一個'else'子句 –

相關問題