例如,如果我有:
A = [1, 2, 3]` & `B = [4, 5, 6]
我想拥有
C = [[1, 4], [2, 5], [3, 6]]
答案 0 :(得分:1)
您可以使用元组和zip满足此要求。
示例代码-
>>> a = [1,2,3]
>>> b = [4,5,6]
>>> c = tuple(zip(a,b))
>>> print(c)
((1, 4), (2, 5), (3, 6))
答案 1 :(得分:1)
为此有一个名为zip的内置函数:
[list(ab) for ab in zip(a,b)]
或使用地图和邮政编码:
list(map(list, zip(a,b)))
两次返回:
[[1, 4], [2, 5], [3, 6]]
答案 2 :(得分:1)
您可以这样做:
a = [1,2,3]
b = [4,5,6]
c = [list(x) for x in zip(a, b)]
答案 3 :(得分:1)
In [110]: A = [1,2,3]
In [111]: B = [4,5,6]
In [112]: list(zip(A,B))
Out[112]: [(1, 4), (2, 5), (3, 6)]