如何并行添加两个嵌套列表并将结果附加到 python 中的新列表
How to add two nested lists in parallel and append result to a new list in python
我正在尝试并行添加两个不相等的嵌套列表的所有元素并将结果追加回另一个新列表,我写了一些可以添加它们但有很多错误的代码使用代码,首先我尝试通过在列表末尾附加 0 来使对相等,但是代码仍然遇到问题,因为第一对的长度是 3,第二对的长度是 4,我也尝试使用地图,但我无法添加整数和 NoneType,
import pdb
import itertools
x = [[2,3,3], [5,0,3]]
y = [[0,3], [2,3,3,3]]
for idx, (a, b) in enumerate(itertools.zip_longest(x, y)):
while len(a) < len(b):
x[idx].append(0)
while len(b) < len(a):
y[idx].append(0)
print(x, y)
new_list = list()
for i in zip(x, y):
for idx, j in enumerate(i):
for ind, a in enumerate(j):
val = x[idx][ind] + y[idx][ind]
new_list.append(val)
print(new_list)
最后的结果应该是这样的
[2, 6, 3, 7, 3, 6, 3]
你可以简单地使用itertools.zip_longest
并填写0
,像这样
>>> from itertools import zip_longest as zip
>>> x = [[2, 3, 3], [5, 0, 3]]
>>> y = [[0, 3], [2, 3, 3, 3]]
>>> [k + l for i, j in zip(x, y, fillvalue=[0]) for k, l in zip(i, j, fillvalue=0)]
[2, 6, 3, 7, 3, 6, 3]
即使 x
和 y
的元素数量不相等,这也会起作用,
>>> from itertools import zip_longest as zip
>>> x = [[2, 3, 3], [5, 0, 3], [1]]
>>> y = [[0, 3], [2, 3, 3, 3]]
>>> [k + l for i, j in zip(x, y, fillvalue=[0]) for k, l in zip(i, j, fillvalue=0)]
[2, 6, 3, 7, 3, 6, 3, 1]
请注意,当我们zip
x
和y
时,我们将[0]
用作fillvalue
。当我们 zip
i
和 j
我们使用 0
作为 fillvalue
.
所以,如果x
和y
中的列表个数不相等,那么[0]
将被用来填充,当[=22中的元素个数时=]和j
不相等,会用0
补全
您可以在 izip_longest
中使用 fillvalue=0
来检查验证,然后使用 map
函数对压缩项目求和:
from itertools import chain,zip_longest
list(chain.from_iterable(map(sum,zip_longest(i,j,fillvalue=0)) for i,j in zip_longest(x, y)))
[2, 6, 3, 7, 3, 6, 3]
请注意,如果您想遍历结果,则不必使用 list
(它只是为了演示结果)。
zip_longest
在这里很有帮助:
x = [[2,3,3], [5,0,3]]
y = [[0,3], [2,3,3,3]]
from itertools import zip_longest
res = []
for list1, list2 in zip_longest(x, y, fillvalue=[0]):
for value1, value2 in zip_longest(list1, list2, fillvalue=0):
res.append(value1 + value2)
填充值用给定值填充列表或子列表。在我们的例子中,一个新列表,外循环为 [0]
,内循环为 0
。
编写嵌套列表推导式的效果相同,但可能需要更多时间来阅读和理解代码。使用更多的行可以使阅读和理解更快。当然,这在很大程度上取决于阅读代码的人。
from itertools import zip_longest
new_list = [a + b
for listpair in zip(x, y)
for a, b in zip_longest(*listpair, fillvalue=0)]
我正在尝试并行添加两个不相等的嵌套列表的所有元素并将结果追加回另一个新列表,我写了一些可以添加它们但有很多错误的代码使用代码,首先我尝试通过在列表末尾附加 0 来使对相等,但是代码仍然遇到问题,因为第一对的长度是 3,第二对的长度是 4,我也尝试使用地图,但我无法添加整数和 NoneType,
import pdb
import itertools
x = [[2,3,3], [5,0,3]]
y = [[0,3], [2,3,3,3]]
for idx, (a, b) in enumerate(itertools.zip_longest(x, y)):
while len(a) < len(b):
x[idx].append(0)
while len(b) < len(a):
y[idx].append(0)
print(x, y)
new_list = list()
for i in zip(x, y):
for idx, j in enumerate(i):
for ind, a in enumerate(j):
val = x[idx][ind] + y[idx][ind]
new_list.append(val)
print(new_list)
最后的结果应该是这样的
[2, 6, 3, 7, 3, 6, 3]
你可以简单地使用itertools.zip_longest
并填写0
,像这样
>>> from itertools import zip_longest as zip
>>> x = [[2, 3, 3], [5, 0, 3]]
>>> y = [[0, 3], [2, 3, 3, 3]]
>>> [k + l for i, j in zip(x, y, fillvalue=[0]) for k, l in zip(i, j, fillvalue=0)]
[2, 6, 3, 7, 3, 6, 3]
即使 x
和 y
的元素数量不相等,这也会起作用,
>>> from itertools import zip_longest as zip
>>> x = [[2, 3, 3], [5, 0, 3], [1]]
>>> y = [[0, 3], [2, 3, 3, 3]]
>>> [k + l for i, j in zip(x, y, fillvalue=[0]) for k, l in zip(i, j, fillvalue=0)]
[2, 6, 3, 7, 3, 6, 3, 1]
请注意,当我们zip
x
和y
时,我们将[0]
用作fillvalue
。当我们 zip
i
和 j
我们使用 0
作为 fillvalue
.
所以,如果x
和y
中的列表个数不相等,那么[0]
将被用来填充,当[=22中的元素个数时=]和j
不相等,会用0
补全
您可以在 izip_longest
中使用 fillvalue=0
来检查验证,然后使用 map
函数对压缩项目求和:
from itertools import chain,zip_longest
list(chain.from_iterable(map(sum,zip_longest(i,j,fillvalue=0)) for i,j in zip_longest(x, y)))
[2, 6, 3, 7, 3, 6, 3]
请注意,如果您想遍历结果,则不必使用 list
(它只是为了演示结果)。
zip_longest
在这里很有帮助:
x = [[2,3,3], [5,0,3]]
y = [[0,3], [2,3,3,3]]
from itertools import zip_longest
res = []
for list1, list2 in zip_longest(x, y, fillvalue=[0]):
for value1, value2 in zip_longest(list1, list2, fillvalue=0):
res.append(value1 + value2)
填充值用给定值填充列表或子列表。在我们的例子中,一个新列表,外循环为 [0]
,内循环为 0
。
编写嵌套列表推导式的效果相同,但可能需要更多时间来阅读和理解代码。使用更多的行可以使阅读和理解更快。当然,这在很大程度上取决于阅读代码的人。
from itertools import zip_longest
new_list = [a + b
for listpair in zip(x, y)
for a, b in zip_longest(*listpair, fillvalue=0)]