Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.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/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 3.x Python在一行中分配两个列表_Python 3.x - Fatal编程技术网

Python 3.x Python在一行中分配两个列表

Python 3.x Python在一行中分配两个列表,python-3.x,Python 3.x,是否可以在同一行中声明两个列表?以下代码包含两个单行程序,因此您必须在c上循环两次: c = [1,2,3,4] a = [ d for d in c if (d % 2 == 0)] b = [ d for d in c if (d % 2 != 0)] 这就是你想要的吗 c = [1, 2, 3, 4] a, b = [d for d in c if (d % 2 == 0)], [d for d in c if (d % 2 != 0)] 或者呢 a, b = [], [] for i

是否可以在同一行中声明两个列表?以下代码包含两个单行程序,因此您必须在
c
上循环两次:

c = [1,2,3,4]
a = [ d for d in c if (d % 2 == 0)]
b = [ d for d in c if (d % 2 != 0)]

这就是你想要的吗

c = [1, 2, 3, 4]
a, b = [d for d in c if (d % 2 == 0)], [d for d in c if (d % 2 != 0)]
或者呢

a, b = [], []
for i in c:
    b.append(i) if i % 2 else a.append(i)

这就是你想要的吗

c = [1, 2, 3, 4]
a, b = [d for d in c if (d % 2 == 0)], [d for d in c if (d % 2 != 0)]
或者呢

a, b = [], []
for i in c:
    b.append(i) if i % 2 else a.append(i)

只有当你不介意这条线十倍难看的时候。只有当你不介意这条线十倍难看的时候。