def noalpha(s):
noa = ''
for c in s:
if not (c in noa or c.isalpha()):
noa += c
return noa
def flines(t, ww):
s=noalpha(t)
lines=t.lower().splitlines()
lst=[]
for i in range(len(lines)):
for caratteri in s:
lines[i]=lines[i].replace(caratteri, ' ')
wrdst=lines[i].split()
l= 'Line ' +str(i)+': '
False
for k in ww:
k=k.lower()
num=wrdst.count(k)
if num>0:
l+='"'+ str(k) + '"=' + str(num)+' '
True
if True:
lst+=[l]
return lst
有人可以向我解釋我如何修改輸出,使空行不出現?輸出運行程序後,我得到的是:如何修改輸出
['Line 0: "to"=1 ', 'Line 1: "the"=1 ', 'Line 2: "the"=1 ', 'Line 3: "to"=1 ', 'Line 4: "so"=2 ', 'Line 5: ', 'Line 6: ', 'Line 7: "to"=1 "the"=1 ', 'Line 8: "to"=1 "the"=2 ', 'Line 9: ', 'Line 10: ', 'Line 11: "to"=1 "the"=1 ', 'Line 12: ', 'Line 13: ', 'Line 14: "so"=1 ', 'Line 15: "to"=1 ', 'Line 16: ', 'Line 17: ', 'Line 18: "to"=1 "the"=2 ', 'Line 19: "the"=1 ', 'Line 20: "the"=4 ', 'Line 21: ', 'Line 22: ', 'Line 23: ', 'Line 24: ', 'Line 25: ', 'Line 26: ', 'Line 27: "the"=1 ', 'Line 28: ', 'Line 29: ', 'Line 30: "the"=1 ', 'Line 31: ']
呃,爲什麼有一個'假'和'真正'只是站在那裏無處? – 2014-10-16 14:58:11
,以便它僅在循環爲真時填充列表 – ProdigySR 2014-10-16 14:59:00
像這樣的代碼僅適用於打印出沒有參數文字的行ww – ProdigySR 2014-10-16 14:59:46