gpt4 book ai didi

python - 检查某些内容是否不在多个列表中 - 如何使其更优雅

转载 作者:行者123 更新时间:2023-11-30 22:12:34 25 4
gpt4 key购买 nike

什么有效:

a = ["0"]
b = ["1"]
c = ["2"]
d = ["3"]
if (not "0" in a) and (not "0" in b) and (not "0" in c) and (not "0" in
d):
print "you don't want this to be printing"

我尝试过但不起作用:

if not "0" in (a and b and c and d):
print ""

if not "0" in a, b, c, d:
print ""

考虑到列表必须是单独的并且我有很多列表,我可以使用什么来避免第一个有效语句的不优雅?

最佳答案

将所有内容放入列表中,然后使用all:

lists = [a, b, c, d]
if all("0" not in x for x in lists):
print("you don't want this to be printing")

但是使用德摩根定律并求反可能更快(也更清晰):

lists = [a, b, c, d]
if not any("0" in x for x in lists):
print("you don't want this to be printing")

更快(最清晰):

if "0" not in set().union(a, b, c, d):
print("you don't want this to be printing")

关于python - 检查某些内容是否不在多个列表中 - 如何使其更优雅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51056081/

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