Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何处理一个条件中的大量元素?_Python - Fatal编程技术网

Python 如何处理一个条件中的大量元素?

Python 如何处理一个条件中的大量元素?,python,Python,我有时需要在一个条件中使用许多元素,所有元素都是相同的类型 args = list() if "a" not in args or "b" not in args or "c" not in args: print("something is missing") 当要测试的元素数量变大时,这会变得复杂。我试图将它们组合在一个列表中,但最终结果很糟糕: args = list() for what in ["a", "b", "c"]: if what not in args:

我有时需要在一个条件中使用许多元素,所有元素都是相同的类型

args = list()
if "a" not in args or "b" not in args or "c" not in args:
    print("something is missing")
当要测试的元素数量变大时,这会变得复杂。我试图将它们组合在一个列表中,但最终结果很糟糕:

args = list()
for what in ["a", "b", "c"]:
    if what not in args:
        print("something is really missing")
        break

用什么样的python方法来编码这种情况(几个组件,都是一样的,如果
)?

使用集合将起作用:

args = list(....)
if set(["a","b","c"]).issubset(args):
    print("something is really missing")

如果顺序无关紧要-即a、b、c可以以任意顺序存在于args中,并且如果args中可能存在其他事物(a、b、c除外),则此操作有效。

执行此操作的典型方法是:


也可以考虑引发错误,而不是打印消息。

<代码>如果不是全部(在“abc”中的X中的ARX中):< /代码>?或者你可以制作它们的集合,并使用那些应该是
集合(“abc”)
集合(“a”、“b”、“c”)
,而你缺少了引号是的,我会在真实的程序中提出一个。这种单行程序构造非常有用,我必须记住它(尽管我喜欢更显式的代码,这是我的Perl时代的一个创伤,在Perl时代,一切都开始正常,然后是“简化”和“单行程序”,代码开始了它自己的生命)@WoJ如果这是您的偏好,那么您现有的构造没有问题!对不起,我的评论不清楚。我一直在寻找你在答案中给出的答案,我只是不知道如何构建它(但看过几次之后,我知道构建是存在的)。总的来说,我只是喜欢在一行程序中装入太多内容时要小心,因为我不明白过了一段时间我写了什么。不过,你的回答很完美,而且切中要害。
if not all(arg in args for arg in ("a", "b", "c")):