【发布时间】:2019-07-21 14:23:43
【问题描述】:
a_set = {'a', 'b', 'c'}
word = 'foobar'
for item in a_set:
if item in word:
print(item)
我想让下面的代码做上面代码的作用
if any(item in lst for item in word):
# print(item)
我更喜欢这种语法,因为它更容易阅读。但是有没有办法检索在 any() 中返回 True 的项目值?或者还有其他功能吗?
【问题讨论】:
-
在 Python 3.8 中将有一个海象运算符可以做到这一点。
-
您正在寻找两个集合的交集。使用
if any(...)是在问一个完全不同的问题,它的答案不是你想要的。