没有得到而不是空列表
我想回来[],但是我没有得到。 例如,当我将现有的str作为关键字时,例如:“赌场”,我会得到适当的结果:[0]。
def word_search(doc_list, keyword):
for i in doc_list:
list = []
words = i.split()
for n in words:
if keyword == n.rstrip('.,').lower():
list.append(doc_list.index(i))
return list
word_search(['The Learn Python Challenge Casino', 'They bought a car, and a horse', 'Casinoville?'], 'ear')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要更加谨慎。当控件在函数末尾掉落而又不遇到
返回列表
时,您将获得一个隐式返回none
。可能是一个不错的第一开始,但是我们可以用
枚举
来做得更好,因此您不需要index
呼叫(而且您也不需要真正使用list
作为变量名称,因为它会为列表的内置类型遮蔽):如果您只喜欢每个文档索引一次出现在结果中:
You'll need to be a bit more careful with your indents. When control falls off the end of the function without encountering that
return list
, you get an implicitreturn None
.might be a good first start, but we can do better with
enumerate
so you don't need theindex
call (and you also don't really want to uselist
as a variable name, since it shadows the built-in type for lists):We can still improve things a bit, if you'd only like each document index to appear once in the results: