【发布时间】:2019-07-01 13:31:41
【问题描述】:
我正在开发一个在 Python3 中定义搜索功能的项目。目标是输出列表中的关键字和 adele.txt 中包含关键字的句子。
这是一个用户定义的列表,userlist=['looking','for','wanna'], adele.txt 在 github 页面上,https://github.com/liuyu82910/search
下面是我的功能。第一个循环是从 adele.txt 中获取所有小写的行,第二个循环是获取 userlist 中的每个小写单词。我的代码没有正确循环。我想要的是循环文本中的所有行并与列表中的所有单词进行比较。我做错了什么?
def search(list):
with open('F:/adele.txt','r') as file:
for line in file:
newline=line.lower()
for word in list:
neword=word.lower()
if neword in newline:
return neword,'->',newline
else:
return False
这是我当前的结果,它停止循环,我只得到一个结果:
Out[122]:
('looking', '->', 'looking for some education\n')
期望的输出是:
'looking', '->', 'looking for some education'
... #there are so many sentences that contain looking
'looking',->'i ain't mr. right but if you're looking for fast love'
...
'for', -> 'looking for some education'
...#there are so many sentences that contain for
'wanna',->'i don't even wanna waste your time'
...
【问题讨论】:
标签: python-3.x for-loop