gpt4 book ai didi

python - 如何压缩numpy数组列表?

转载 作者:行者123 更新时间:2023-12-03 23:11:43 35 4
gpt4 key购买 nike

我有一个 numpy 数组的字典:

{'data1': array([[0.16461831, 0.82400555],
[0.02958593, 0.483629 ],
[0.51268564, 0.07030046],
[0.17027816, 0.35304705]]),
'data2': array([[0.8292598 , 0.78136548],
[0.30389913, 0.69250432],
[0.66608852, 0.42237639],
[0.72678807, 0.40486951]]),
'data3': array([[0.45614633, 0.96677904],
[0.87066105, 0.75826116],
[0.39431988, 0.73041888],
[0.65685809, 0.65498308]])}
预期输出:
[([0.16461831, 0.82400555], [0.8292598 , 0.78136548], [0.45614633, 0.96677904]), 
([0.02958593, 0.483629 ], [0.66608852, 0.42237639], [0.87066105, 0.75826116]),
([0.51268564, 0.07030046], [0.66608852, 0.42237639], [0.39431988, 0.73041888]),
([0.17027816, 0.35304705], [0.72678807, 0.40486951], [0.65685809, 0.65498308])]
但是当我尝试使用 zip 时:
list(zip(data.values()))
得到这个输出:
[(array([[0.16461831, 0.82400555],
[0.02958593, 0.483629 ],
[0.51268564, 0.07030046],
[0.17027816, 0.35304705]]),),
(array([[0.8292598 , 0.78136548],
[0.30389913, 0.69250432],
[0.66608852, 0.42237639],
[0.72678807, 0.40486951]]),),
(array([[0.45614633, 0.96677904],
[0.87066105, 0.75826116],
[0.39431988, 0.73041888],
[0.65685809, 0.65498308]]),)]
如何压缩numpy数组列表?

最佳答案


list(zip(*data.values())
输出:
[(array([0.16461831, 0.82400555]),
array([0.8292598 , 0.78136548]),
array([0.45614633, 0.96677904])),
(array([0.02958593, 0.483629 ]),
array([0.30389913, 0.69250432]),
array([0.87066105, 0.75826116])),
(array([0.51268564, 0.07030046]),
array([0.66608852, 0.42237639]),
array([0.39431988, 0.73041888])),
(array([0.17027816, 0.35304705]),
array([0.72678807, 0.40486951]),
array([0.65685809, 0.65498308]))]

关于python - 如何压缩numpy数组列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62838594/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com