【发布时间】:2012-08-29 14:02:41
【问题描述】:
可能重复:
Python Check if all of the following items is in a list
所以我想测试一下 word 和 word1 是否都在列表 lst 中。 当然,我可以写:
if word in lst and word1 in lst:
do x
但我想知道是否可以将该语句缩短为:
if (word and word1) in lst:
do x
当然,这是行不通的,但是有什么有效的类似的东西吗?
我尝试了以下方法,但如您所见,它没有产生预期的结果。
>>> word in lst
True
>>> word1 in lst
True
>>> (word, word1) in lst
False
编辑:谢谢你的回答,我想我现在对如何做到这一点有了一个很好的想法。
【问题讨论】:
-
试试
set(lst).issubset([word, word1])stackoverflow.com/questions/3931541/…
标签: python python-3.x conditional-operator