为了账号安全,请及时绑定邮箱和手机立即绑定

使用字典找到文件中出现单词的行号

使用字典找到文件中出现单词的行号

慕田峪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 关注
  • 178 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号