Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/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_List_While Loop_Int_Expression - Fatal编程技术网

使用';和';表达式在';而';Python中的循环

使用';和';表达式在';而';Python中的循环,python,list,while-loop,int,expression,Python,List,While Loop,Int,Expression,在我的程序中,我有一个列表,我想要一个循环,直到列表的所有列都等于2。列中的所有项都是数字,但有些项的格式为字符串,有些项的格式为整数,因为它们在程序的其他部分会发生更改。 下面是我尝试过的3个解决方案,我正在运行Python2.7 while (int(newlist[0][1]) != 2) and (int(newlist[1][1]) != 2) etc... != 2: 我遇到的问题是,当只有一个列表项(而不是整个列)等于2时,循环就结束了 如果有人能告诉我我做错了什么或者有更好的方

在我的程序中,我有一个列表,我想要一个循环,直到列表的所有列都等于2。列中的所有项都是数字,但有些项的格式为字符串,有些项的格式为整数,因为它们在程序的其他部分会发生更改。 下面是我尝试过的3个解决方案,我正在运行Python2.7

while (int(newlist[0][1]) != 2) and (int(newlist[1][1]) != 2) etc... != 2:
我遇到的问题是,当只有一个列表项(而不是整个列)等于2时,循环就结束了


如果有人能告诉我我做错了什么或者有更好的方法,我会非常感谢你的帮助

括号中的表达式被计算为一个值:
newlist[0-8]
newlist[-8]
相同,
newlist[0和1、2和3]
newlist[0]
相同。你想要:


尝试使用for循环。假设
L
是您的列表,现在我们可以迭代行,每个行本身就是一个列表,如下所示:

column = 1
for row in L:
  while int(row[column]) != 2:
    do_this()
    increase_column()

确保while循环通过增加row[column]来终止,从而最终使while测试失败。

在第一个示例中,如果有任何项不是2,则希望
while
循环继续。因此,您希望使用
而不是
。或者,你可以在不写的时候写
(something==2,something_else==2,and…)
@LukeWoodward:我正要写这个!)我想询问者可能是在寻找
any
。如果
while
循环应该在所有项目都是2时终止,那么如果任何项目不是2,它应该继续运行。这就是我一直在寻找的,谢谢phihag和Luke Woodward!
while any(int(newsublist[1]) != 2 for newsublist in newlist):
column = 1
for row in L:
  while int(row[column]) != 2:
    do_this()
    increase_column()