gpt4 book ai didi

列表理解中的 Python any() 函数

转载 作者:太空狗 更新时间:2023-10-29 18:27:55 25 4
gpt4 key购买 nike

我是 Python 的新手(2 周!)并且遇到以下问题:

我有一个 URL 列表,我想遍历这些 URL 并找到特定的 URL。去做这个我想测试 URL 中是否存在元组的任何成员。

我发现我需要一个 any() 语句,但语法不正确:

allurls = [<big list of URLs>]

words = ('bob', 'fred', 'tom')

urlsIwant = [x for x in allurls if any(w for w in words) in x]

让我明白

TypeError: 'in <string>' requires string as left operand, not bool

我认为这不相关,但我的实际代码是

urlsIwant = sorted(set([x for x in allurls if dict['value'] in x and any(w for w in words) in x]))

最佳答案

any() 中包含 in x:

urlsIwant = [x for x in allurls if any(w in x for w in words)]

关于列表理解中的 Python any() 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23755082/

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