gpt4 book ai didi

python - 使用字典查找文件中单词出现的行号

转载 作者:太空宇宙 更新时间:2023-11-03 18:57:34 25 4
gpt4 key购买 nike

因此,对于家庭作业,我必须找到单词(由列表给出)出现的行号或数字。到目前为止我有这个

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]))

所以我到达这里并收到错误,因为它超出了范围。我已经工作了很长时间,因为现在一切看起来都是胡言乱语。

最佳答案

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

这里还有一些提示可以使您的代码更简洁,但目前可能太高级:enumeratedefaultdict(list)

关于python - 使用字典查找文件中单词出现的行号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16930064/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com