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
..
請問你想始終更換次數是字符串的第一個值? – Onlyjus
當涉及到該列表中的任何內容時,是的。但是我還會遇到其他情況,在整個代碼中需要隨時更換['50':'Fifty']。 – Matthew