将元组列表转换为二维Numpy数组 [英] Convert List of List of Tuples Into 2d Numpy Array
本文介绍了将元组列表转换为二维Numpy数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个元组列表的列表:
I have a list of list of tuples:
X = [[(0.5, 0.5, 0.5), (1.0, 1.0, 1.0)], [(0.5, 0.5, 0.52), (1.0, 1.0, 1.0)], [(0.5, 0.52, 0.52), (1.0, 1.0, 1.0)], [(0.52, 0.52, 0.52), (1.0, 1.0, 1.0)], [(0.52, 0.52, 0.52), (1.0, 1.0, 1.0)]]
,并希望将其转换为2D numpy数组.我尝试了以下操作,但没有成功.我只想确保新的变换后的2D保持X的形状和结构.请帮助我,非常感谢.
and would like to convert it to a 2D numpy array. I tried the following but didn't work. I just want to make sure the new transformed 2D keeps the same shape and structure of X. Please help me , thanks a lot.
import numpy as np
X = [[(0.5, 0.5, 0.5), (1.0, 1.0, 1.0)], [(0.5, 0.5, 0.52), (1.0, 1.0, 1.0)], [(0.5, 0.52, 0.52), (1.0, 1.0, 1.0)], [(0.52, 0.52, 0.52), (1.0, 1.0, 1.0)], [(0.52, 0.52, 0.52), (1.0, 1.0, 1.0)]]
np.asarray([sublist for sublist in X])
预期结果将是:
[[ 0.5 , 0.5 , 0.5 ],
[ 1. , 1. , 1. ]],
[[ 0.5 , 0.5 , 0.52],
[ 1. , 1. , 1. ]],
[[ 0.5 , 0.52, 0.52],
[ 1. , 1. , 1. ]],
[[ 0.52, 0.52, 0.52],
[ 1. , 1. , 1. ]],
[[ 0.52, 0.52, 0.52],
[ 1. , 1. , 1. ]]
推荐答案
不知道您是在使用python 2.x还是3.x,但是在3.x中可以尝试:
Dont know if you are working on python 2.x or 3.x but in 3.x you could try:
>> import numpy as np
>> X = [(....)] # Your list
>> array = np.array([*X])
>> print(array)
array([[[ 0.5 , 0.5 , 0.5 ],
[ 1. , 1. , 1. ]],
[[ 0.5 , 0.5 , 0.52],
[ 1. , 1. , 1. ]],
[[ 0.5 , 0.52, 0.52],
[ 1. , 1. , 1. ]],
[[ 0.52, 0.52, 0.52],
[ 1. , 1. , 1. ]],
[[ 0.52, 0.52, 0.52],
[ 1. , 1. , 1. ]]])
不知道这是您要实现的目标.
Dont know if this is what you want to achieve.
这篇关于将元组列表转换为二维Numpy数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文