因此,对于作业,我必须找到一个或多个行的数字(由列表给出)。到目前为止我有这个
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]))
所以我到了这里,因为超出范围我得到了错误。我一直在努力,因为它现在看起来像乱码。
答案 0 :(得分:1)
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)
答案 1 :(得分:0)
因为它的功课我只会用算法来帮助你
foreach line in the_file:
if search_word in line:
print line_number
这离python已经只有一小步了