我有列表L1 = [123,543]
,L2 = [321,279]
。使用Zip,我们可以合并两个列表
n = [list(a) for a in zip(L1,L2)]
for i in (n):
print (i)
输出:
[123, 321]
[543, 279]
如何找到python中List元素之间的距离?
示例:123
和321
之间的差为0
(2 + 0 -2)。
543
和279
之间的差是6
(-3 + 3 + 6)。
答案 0 :(得分:0)
您可以再次zip
查找数字差并求和:
L1 = [123,543]
L2 = [321,279]
lst = []
for x, y in zip(L1, L2):
lst.append(sum(int(b) - int(a) for a, b in zip(str(x), str(y))))
print(lst)
# [0, 6]
答案 1 :(得分:0)
尝试这个:
In [37]: for i,j in zip(L1, L2):
...: b = list(map(int,list(str(i))))
...: c = list(map(int,list(str(j))))
...: d = [x1 - x2 for (x1, x2) in zip(b, c)]
...: print(sum(d))