没有得到而不是空列表

发布于 2025-02-13 11:36:08 字数 473 浏览 2 评论 0 原文

我想回来[],但是我没有得到。 例如,当我将现有的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')

I want to get back [], but instead i get None.
When i put existing str as a keyword, for example: 'casino', i get a proper result: [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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

归属感 2025-02-20 11:36:09

您需要更加谨慎。当控件在函数末尾掉落而又不遇到返回列表时,您将获得一个隐式返回none

def word_search(doc_list, keyword):
    list = []
    for i in doc_list:
        words = i.split()
        for n in words:
            if keyword == n.rstrip('.,').lower():
                list.append(doc_list.index(i))
    return list

可能是一个不错的第一开始,但是我们可以用枚举来做得更好,因此您不需要 index 呼叫(而且您也不需要真正使用 list 作为变量名称,因为它会为列表的内置类型遮蔽):

def word_search(doc_list, keyword):
    results = []
    for i, doc in enumerate(doc_list):
        words = doc.split()
        for n in words:
            if keyword == n.rstrip('.,').lower():
                results.append(i)
    return results 

如果您只喜欢每个文档索引一次出现在结果中:

def word_search(doc_list, keyword):
    results = []
    for i, doc in enumerate(doc_list):
        if any(keyword == word.rstrip('.,').lower() for word in doc.split()):
            results.append(i)
    return results 

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 implicit return None.

def word_search(doc_list, keyword):
    list = []
    for i in doc_list:
        words = i.split()
        for n in words:
            if keyword == n.rstrip('.,').lower():
                list.append(doc_list.index(i))
    return list

might be a good first start, but we can do better with enumerate so you don't need the index call (and you also don't really want to use list as a variable name, since it shadows the built-in type for lists):

def word_search(doc_list, keyword):
    results = []
    for i, doc in enumerate(doc_list):
        words = doc.split()
        for n in words:
            if keyword == n.rstrip('.,').lower():
                results.append(i)
    return results 

We can still improve things a bit, if you'd only like each document index to appear once in the results:

def word_search(doc_list, keyword):
    results = []
    for i, doc in enumerate(doc_list):
        if any(keyword == word.rstrip('.,').lower() for word in doc.split()):
            results.append(i)
    return results 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文