Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.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_Python 3.x_If Statement_Conditional - Fatal编程技术网

Python 从列表中生成多个条件语句

Python 从列表中生成多个条件语句,python,python-3.x,if-statement,conditional,Python,Python 3.x,If Statement,Conditional,我有一张这样的清单 a = [1,0,6,9,6,0,6,0,1,4,0,7,5,0] 我想创建如下条件语句: if a[1,3,8,13] != 0: do something if a[1] != 0 and a[3] != 0 and a[8] != 0 and a[13] != 0: do something 这个代码显然是错误的。它必须是这样的: if a[1,3,8,13] != 0: do something if a[1] != 0 and a[3

我有一张这样的清单

a = [1,0,6,9,6,0,6,0,1,4,0,7,5,0]
我想创建如下条件语句:

if a[1,3,8,13] != 0:
    do something
if a[1] != 0 and a[3] != 0 and a[8] != 0 and a[13] != 0:
    do something
这个代码显然是错误的。它必须是这样的:

if a[1,3,8,13] != 0:
    do something
if a[1] != 0 and a[3] != 0 and a[8] != 0 and a[13] != 0:
    do something
我想知道在我的例子中,是否有更优雅的方法来使用lambda或loop编写多个条件语句。假设我的列表长度大约为100,我需要为列表中的57列做一个条件语句。我可能不想用这种方式把它们都写出来。。。 谢谢。 您可以使用
all

if all(a[i] != 0 for i in [1,3,8,13]): # => a[i] != 0 for every i 
    #do smth
或者与
any
相同:

if not any(a[i] == 0 for i in [1,3,8,13]): # => there is no such i that a[i] == 0
    #do smth                               # => a[i] != 0 for every i 

2. 或者,您可以使用一些函数式编程工具。
例如,您可以尝试
过滤器

a = [1,0,6,9,6,0,6,0,1,4,0,7,5,0]
idxs_to_check = [1,3,8,13]

f = list(filter(lambda i: a[i] != 0, idxs_to_check))
if f == idxs_to_check:
    print('Meow')
或者,在一行中出现类似情况:

if not list(filter(lambda i: a[i] == 0, [1,3,8,13])): 
    print('Meow')

3. 最后,在您的特定情况下,您可以使用包含零的列表的乘积为零的事实:

from numpy import prod

if prod([a[i] for i in [1,3,8,13]]):
    print("Woof")
1. 您可以使用
all

if all(a[i] != 0 for i in [1,3,8,13]): # => a[i] != 0 for every i 
    #do smth
或者与
any
相同:

if not any(a[i] == 0 for i in [1,3,8,13]): # => there is no such i that a[i] == 0
    #do smth                               # => a[i] != 0 for every i 

2. 或者,您可以使用一些函数式编程工具。
例如,您可以尝试
过滤器

a = [1,0,6,9,6,0,6,0,1,4,0,7,5,0]
idxs_to_check = [1,3,8,13]

f = list(filter(lambda i: a[i] != 0, idxs_to_check))
if f == idxs_to_check:
    print('Meow')
或者,在一行中出现类似情况:

if not list(filter(lambda i: a[i] == 0, [1,3,8,13])): 
    print('Meow')

3. 最后,在您的特定情况下,您可以使用包含零的列表的乘积为零的事实:

from numpy import prod

if prod([a[i] for i in [1,3,8,13]]):
    print("Woof")

你可以试试类似的东西

a = [1,0,6,9,6,0,6,0,1,4,0,7,5,0]

if a[1] or a[2] or a[8] or a[13] != a[2]:
      print (“something”) #this part cuz I’m not sure what ur doing here
我真的不知道这是否管用,希望它管用。 但这可能会再次重复。所以你可能想得到

if all(a[i] != 0 for i in [1,3,8,13]
基本上,ur在列表中迭代每个ur选择的位置,以检查位置0是否相同


希望这有帮助

您可以尝试以下方法

a = [1,0,6,9,6,0,6,0,1,4,0,7,5,0]

if a[1] or a[2] or a[8] or a[13] != a[2]:
      print (“something”) #this part cuz I’m not sure what ur doing here
我真的不知道这是否管用,希望它管用。 但这可能会再次重复。所以你可能想得到

if all(a[i] != 0 for i in [1,3,8,13]
基本上,ur在列表中迭代每个ur选择的位置,以检查位置0是否相同

希望这有帮助