这些都是清单。
[(3.8, 101.39), (1.597, 103.613), (4.7, 100.83), (4.804, 101.52)]
[(3.845, 101.396), (1.56, 103.613), (4.71, 100.9), (4.454, 101.161)]
[(3.847, 101.3), (1.59, 103.63), (4.76, 100.89), (4.45, 101.16)]
怎么能把它们分割成这样呢?
[(3.8, 101.39), (3.845, 101.396), (3.847, 101.3)]
[(1.597, 103.613), (1.56, 103.613), (1.59, 103.63)]
[(4.7, 100.83), (4.71, 100.9), (4.76, 100.89)]
[(4.804, 101.52), (4.454, 101.161), (4.45, 101.16)]
并最终能够产生。
3.8 101.39 3.845 101.396 3.847 101.3
1.597 103.613 1.56 103.613 1.59 103.63
4.7 100.83 4.71, 100.9 4.76 100.89
4.804 101.52 4.454 101.161 4.45 101.16
我现在的代码:
a, b, c = map(list, zip(*abc))
d, e = map(list, zip(*a))
产生这样的结果:
[3.8462193, 1.597, 4.7, 4.804 ]
[101.39, 103.613, 100.83, 101.52]
这是错的...
如果我没理解错的话,你需要做的就是用第一个 map(list, zip(*abc))
它应该正确地分割输入,以得到你想要的结果。虽然我认为你的代码有一个错误,因为那行应该输出4个变量,而不是3个,因此它应该只是。
abc = [
[(3.8, 101.39), (1.597, 103.613), (4.7, 100.83), (4.804, 101.52)],
[(3.845, 101.396), (1.56, 103.613), (4.71, 100.9), (4.454, 101.161)],
[(3.847, 101.3), (1.59, 103.63), (4.76, 100.89), (4.45, 101.16)]
]
a, b, c, d= map(list, zip(*abc))
print(a) # [(3.8, 101.39), (3.845, 101.396), (3.847, 101.3)]
print(b) # [(1.597, 103.613), (1.56, 103.613), (1.59, 103.63)]
print(c) # [(4.7, 100.83), (4.71, 100.9), (4.76, 100.89)]
print(d) # [(4.804, 101.52), (4.454, 101.161), (4.45, 101.16)]
另外,你从来没有定义过什么变量 de
是所以我不确定我知道你到底想达到什么目的,但我希望这是你想做的。
你所描述的是一个 矩阵转置你可以通过列表理解来实现。
abc = [
[(3.8, 101.39), (1.597, 103.613), (4.7, 100.83), (4.804, 101.52)],
[(3.845, 101.396), (1.56, 103.613), (4.71, 100.9), (4.454, 101.161)],
[(3.847, 101.3), (1.59, 103.63), (4.76, 100.89), (4.45, 101.16)]
]
transpose = [[row[i] for row in abc] for i in range(len(abc[0]))]
为了以线性的方式打印所有的内容, 你可以遍历所有的列表并在适当的地方插入新的一行:
for line in transpose:
for row in line:
print(row[0], row[1], end="")
print()
然后你就能得到预期的结果
3.8 101.393.845 101.3963.847 101.3
1.597 103.6131.56 103.6131.59 103.63
4.7 100.834.71 100.94.76 100.89
4.804 101.524.454 101.1614.45 101.16
这里有一个 numpy
基于的解决方案,如果你想尝试。
使用。
import numpy as np
abc = [[(3.8, 101.39), (1.597, 103.613), (4.7, 100.83), (4.804, 101.52)],
[(3.845, 101.396), (1.56, 103.613), (4.71, 100.9), (4.454, 101.161)],
[(3.847, 101.3), (1.59, 103.63), (4.76, 100.89), (4.45, 101.16)]]
arr = np.array(abc)
a, b, c, d = map(lambda x: x.flatten().tolist(), np.hsplit(arr, 4))
结果:
[3.8, 101.39, 3.845, 101.396, 3.847, 101.3] # list a
[1.597, 103.613, 1.56, 103.613, 1.59, 103.63] # list b
[4.7, 100.83, 4.71, 100.9, 4.76, 100.89] # list c
[4.804, 101.52, 4.454, 101.161, 4.45, 101.16] # list d