亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用字典找到文件中出現單詞的行號

使用字典找到文件中出現單詞的行號

慕田峪7331174 2021-03-11 10:10:30
因此,對于家庭作業,我必須找到出現在單詞上的行號(由列表給出)。到目前為止,我有這個def index(f,l):   'str,list(str)==nonetype'    infile=open(f)    file=infile.read()    b=file.split('\n')    G=file.splitlines()    infile.close    count=1    res=0    c={}    a=[]    for i in b:        a+=[i]    for n in l:        while res <len(G):            small={G[res]:count}            c.update(small)            count+=1            res+=1            if (a[res] in c) and (n in a[res]):                print (n+'{}'.format(c[count]))   所以我到達這里,因為它超出了范圍,所以我得到了錯誤。我一直在努力,因為這一切現在看起來像胡言亂語。
查看完整描述

2 回答

?
小唯快跑啊

TA貢獻1863條經驗 獲得超2個贊

def index(filename, wordlist):


    # Initialize some variable with meaningful names


    with open(filename) as infile:

        for line in infile:

            # Do something with the line


    return # something

這里有一些更多的提示,可以讓你的代碼更簡潔,但可能過于超前現在:enumerate,defaultdict(list)


查看完整回答
反對 回復 2021-03-30
  • 2 回答
  • 0 關注
  • 188 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號