python - 在双重条件下应用 AND 运算符

标签 python

我正在尝试应用 AND 运算符来观察 python 中的双重条件,但它似乎没有按预期工作。这是一个例子;

ylist=[(u'it', u'is'), (u'speak', u'of'), (u'of', u'school')]
slist=['it', 'is', 'of', 'not']

[x for x in ylist if (x[0] and x[1]) not in slist]

output:[(u'of', u'school')]

Expected output:[(u'speak', u'of'),(u'of', u'school')]

感谢您的建议。

最佳答案

您需要使用any来减少元组中每个项目的不包含测试:

>>> ylist=[(u'it', u'is'), (u'speak', u'of'), (u'of', u'school')]
>>> slist=['it', 'is', 'of', 'not']
>>> [x for x in ylist if any(e not in slist for e in x)]
[(u'speak', u'of'), (u'of', u'school')]

关于python - 在双重条件下应用 AND 运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43479377/

相关文章:

python - 如何在Python中将列表的一部分转换为整数?

python - 卫星图像分类 Opencv

python - Boto3 cron : Parameter ScheduleExpression is not valid

Python Selenium - WebDriverWait 直到显示任一标题

python - 如何防止 csv.DictWriter() 或 writerow() 舍入我的 float ?

python - django 2 mysql 数据库后端不接受 0 作为 AutoField 错误的值

python - 在 Debian 操作系统上使用 pandas 从 github 存储库读取 csv 文件时出现奇怪的 csv 输出

python - 查看 Tkinter 小部件是否存在(不使用 .winfo_exists)

python - 如果不调用super().__ init __(),自定义异常类的父级如何获取参数?

python - 在元组列表中查找索引