Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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_List_For Loop_If Statement - Fatal编程技术网

Python 非期望整数的测试列表

Python 非期望整数的测试列表,python,list,for-loop,if-statement,Python,List,For Loop,If Statement,我有一张单子 list1 = [0,1,0,0] 如果“1”在列表中,但在其他情况下继续,如何创建打印“Failed!”的if语句 其他例子 list2 = [1,1,0,0] list3 = [0,0,0,0] 列表中可以有更多或更少的整数。要测试列表中的对象,只需使用语法如果我的列表中的x:其中x是您要测试的对象,比如1或0。如果只有0和1,您可以使用如果sum(listname)>0:print“Failed”如果iterable的任何元素为True,则返回True。如果iterabl

我有一张单子

list1 = [0,1,0,0]
如果“1”在列表中,但在其他情况下继续,如何创建打印“Failed!”的if语句

其他例子

list2 = [1,1,0,0]
list3 = [0,0,0,0]

列表中可以有更多或更少的整数。

要测试列表中的对象,只需使用语法
如果我的列表中的x:
其中x是您要测试的对象,比如1或0。

如果只有0和1,您可以使用
如果sum(listname)>0:print“Failed”

如果iterable的任何元素为True,则返回True。如果iterable为空,则返回False

假设您只想显示一次“失败”,并且只有0和1:

if any(listname):
    print "Failed"

它干净易读。如果还有其他整数,
如果列表名中有1将是最简单的解决方案。

如果需要计算一个项目在列表中出现的次数,应使用“计数”。例如:

>> a = [1,2,3,3,2,2]
>> a.count(2)
3

你为什么不干脆用它呢

if 1 in list_name: print 'failed' //break here if you want else: //continue your code` 如果列表名称中有1: 打印“失败” //如果你愿意,就在这里休息 其他: //继续你的代码`