【发布时间】:2021-06-02 15:32:19
【问题描述】:
我有以下列表:
Lat = [[1, 2], [3, 4]]
Lat = [[5, 6], [7, 8]]
而且,我想要这个Final = [[(1,5), (2,6) ], [(3,7), (4,8)]],
其中两个列表都由,隔开
这在 python 中可行吗?
【问题讨论】:
我有以下列表:
Lat = [[1, 2], [3, 4]]
Lat = [[5, 6], [7, 8]]
而且,我想要这个Final = [[(1,5), (2,6) ], [(3,7), (4,8)]],
其中两个列表都由,隔开
这在 python 中可行吗?
【问题讨论】:
这是一个不错的单行线:
x = [[1, 2], [3, 4]]
y = [[5, 6], [7, 8]]
result = [[(item[0][idx], item[1][idx]) for idx in range(len(x))] for item in zip(x, y)]
# [[(1, 5), (2, 6)], [(3, 7), (4, 8)]]
假设x 和y 具有相同的长度(假设发生在我写range(len(x)) 的地方。
【讨论】:
ln=[]
for i,n in enumerate(lat1):
ln.append([(n[0],lat2[i][0]),(n[1],lat2[i][1])])
【讨论】:
您可以使用zip()
l1 = [[1, 2], [3, 4]]
l2 = [[5, 6], [7, 8]]
res = [[(y[0], y[1]) for y in zip(*x)] for x in zip(l1, l2)]
print(res)
输出:
[(1, 5), (2, 6), (3, 7), (4, 8)]
【讨论】: