我有一个列表列表,列表的第三个元素如下所示:
[
[[-0.8690725419806353, -0.08389000630791843, 0.7791382771921971, -0.21049789958222198], [-0.7007253732065291, 0.99458521937916, -0.5468526445976842, -0.3094246565483987]],
[[0.584256147560986, -0.5642927958007717, 0.21759356280359254, -0.06807820714092316], [-0.9779032680122586, 0.24546752298204022, 0.3751715219217411, -0.3146135836256405]],
[[-0.5398355639736792, 0.4502589522424809, 0.9293381308257145, -0.10657822991597832], [-0.6113730000884487, 0.9140987217023038, 0.251920069381135, 0.42646522786278185]],
[[-0.23324198103994065, 0.09276611770008003, -0.5799583815644322, 0.23100380680142574], [-0.679057196855118, -0.7221510084412031, 0.5840055827718216, 0.1973048386398748]],
[[0.43867699536415694, 0.27146397192191607, 0.7011538998707785, 0.8932145525910065], [0.09131111620483745, -0.14453114049637228, 0.6039831172025221, 0.7754730090802249]],
[[0.6802994135674767, -0.2327763818549724, -0.3282557058999114, -0.2724537918871204], [0.7147975426033915, -0.9615052173793399, -0.3368913254473691, -0.4013739657188222]]
]
上面的数据是列表的第3个元素,我想提取第3个元素中每个列表的第一个元素,所以最后我会得到类似的东西
list1 = [ -0.8690725419806353,0.584256147560986,-0.5398355639736792,-0.23324198103994065,0.43867699536415694,0.6802994135674767]
list2 = [ -0.08389000630791843,-0.5642927958007717,0.4502589522424809,0.09276611770008003,0.27146397192191607,-0.2327763818549724]
...等等
如何从我的数据结构中提取这些数据? (请注意,上面的数据已经是主根列表中的第三个元素,因此也需要从中提取它的[3]元素)
答案 0 :(得分:0)
arr = [
[[-0.8690725419806353, -0.08389000630791843, 0.7791382771921971, -0.21049789958222198], [-0.7007253732065291, 0.99458521937916, -0.5468526445976842, -0.3094246565483987]],
[[0.584256147560986, -0.5642927958007717, 0.21759356280359254, -0.06807820714092316], [-0.9779032680122586, 0.24546752298204022, 0.3751715219217411, -0.3146135836256405]],
[[-0.5398355639736792, 0.4502589522424809, 0.9293381308257145, -0.10657822991597832], [-0.6113730000884487, 0.9140987217023038, 0.251920069381135, 0.42646522786278185]],
[[-0.23324198103994065, 0.09276611770008003, -0.5799583815644322, 0.23100380680142574], [-0.679057196855118, -0.7221510084412031, 0.5840055827718216, 0.1973048386398748]],
[[0.43867699536415694, 0.27146397192191607, 0.7011538998707785, 0.8932145525910065], [0.09131111620483745, -0.14453114049637228, 0.6039831172025221, 0.7754730090802249]],
[[0.6802994135674767, -0.2327763818549724, -0.3282557058999114, -0.2724537918871204], [0.7147975426033915, -0.9615052173793399, -0.3368913254473691, -0.4013739657188222]]
]
flat = [xss[0] for xss in arr]
transpose = list(zip(*flat))
结果:
[(-0.8690725419806353,
0.584256147560986,
-0.5398355639736792,
-0.23324198103994065,
0.43867699536415694,
0.6802994135674767),
(-0.08389000630791843,
-0.5642927958007717,
0.4502589522424809,
0.09276611770008003,
0.27146397192191607,
-0.2327763818549724),
(0.7791382771921971,
0.21759356280359254,
0.9293381308257145,
-0.5799583815644322,
0.7011538998707785,
-0.3282557058999114),
(-0.21049789958222198,
-0.06807820714092316,
-0.10657822991597832,
0.23100380680142574,
0.8932145525910065,
-0.2724537918871204)]
答案 1 :(得分:0)
使用此代码:
main = [
[[-0.8690725419806353, -0.08389000630791843, 0.7791382771921971,
-0.21049789958222198], [-0.7007253732065291, 0.99458521937916, -0.5468526445976842,
-0.3094246565483987]],
[[0.584256147560986, -0.5642927958007717, 0.21759356280359254, -0.06807820714092316],
[-0.9779032680122586, 0.24546752298204022, 0.3751715219217411, -0.3146135836256405]],
[[-0.5398355639736792, 0.4502589522424809, 0.9293381308257145, -0.10657822991597832],
[-0.6113730000884487, 0.9140987217023038, 0.251920069381135, 0.42646522786278185]],
[[-0.23324198103994065, 0.09276611770008003, -0.5799583815644322,
0.23100380680142574], [-0.679057196855118, -0.7221510084412031, 0.5840055827718216,
0.1973048386398748]],
[[0.43867699536415694, 0.27146397192191607, 0.7011538998707785, 0.8932145525910065],
[0.09131111620483745, -0.14453114049637228, 0.6039831172025221, 0.7754730090802249]],
[[0.6802994135674767, -0.2327763818549724, -0.3282557058999114, -0.2724537918871204],
[0.7147975426033915, -0.9615052173793399, -0.3368913254473691, -0.4013739657188222]]
]
ListNo = 1
index = 0
while (len(main[0][0]) > index):
lists = []
for l in main:
lists.append(l[0][index])
index += 1
print("List "+str(ListNo)+ ": ",lists)
ListNo += 1
index = 0
while (len(main[0][1]) > index):
lists1 = []
for l in main:
lists1.append(l[1][index])
index += 1
print("List "+str(ListNo)+ ": ",lists1)
ListNo += 1
输出:
List 1: [-0.8690725419806353, 0.584256147560986, -0.5398355639736792,
-0.23324198103994065, 0.43867699536415694, 0.6802994135674767]
List 2: [-0.08389000630791843, -0.5642927958007717, 0.4502589522424809,
0.09276611770008003, 0.27146397192191607, -0.2327763818549724]
List 3: [0.7791382771921971, 0.21759356280359254, 0.9293381308257145,
-0.5799583815644322, 0.7011538998707785, -0.3282557058999114]
List 4: [-0.21049789958222198, -0.06807820714092316, -0.10657822991597832,
0.23100380680142574, 0.8932145525910065, -0.2724537918871204]
List 5: [-0.7007253732065291, -0.9779032680122586, -0.6113730000884487,
-0.679057196855118, 0.09131111620483745, 0.7147975426033915]
List 6: [0.99458521937916, 0.24546752298204022, 0.9140987217023038,
-0.7221510084412031, -0.14453114049637228, -0.9615052173793399]
List 7: [-0.5468526445976842, 0.3751715219217411, 0.251920069381135,
0.5840055827718216, 0.6039831172025221, -0.3368913254473691]
List 8: [-0.3094246565483987, -0.3146135836256405, 0.42646522786278185,
0.1973048386398748, 0.7754730090802249, -0.4013739657188222]