我有一个嵌套的元组列表:
tuple_list = [[(0, 0.022190866145025672),
(1, 0.03713307553569147),
(2, 0.03329292095418187),
(3, 0.013397487788645558),
(4, 0.012968754425823006),
(5, 0.02303938132767878),
(6, 0.5055000245070915),
(7, 0.02148198409145118)],
[(0, 0.027141399289287292),
(1, 0.034094330130799384),
(2, 0.18793352530921031),
(3, 0.01545954116652308),
(4, 0.03658418672184262),
(5, 0.1929824217980975),
(6, 0.021051003033754387),
(7, 0.03489076576637312)]]
正如您所能看到的,嵌套列表具有相同的长度
是否可以将其转换为numpy数组
我已经尝试将其转换为如下numpy数组:
y = numpy.array(tuple_list)
但我得到的结果是:
array([list([(0, 0.022190866145025672), (1, 0.03713307553569147), (2, 0.03329292095418187), (3, 0.013397487788645558), (4, 0.012968754425823006), (5, 0.02303938132767878), (6, 0.5055000245070915), (7, 0.02148198409145118), (8, 0.028544808844256947), (9, 0.023656766961056515), (10, 0.058975765442311284), (11, 0.052628865356187236), (12, 0.019632103194206267), (13, 0.017715501743987942), (14, 0.013181486857172771), (15, 0.03693920037515881), (16, 0.059811388273952625), (17, 0.019909618176120435)]),
list([(0, 0.027141399289287292), (1, 0.034094330130799384), (2, 0.18793352530921031), (3, 0.01545954116652308), (4, 0.03658418672184262), (5, 0.1929824217980975), (6, 0.021051003033754387), (7, 0.03489076576637312), (8, 0.027263833771125616), (9, 0.022274146808526914), (10, 0.1321132498202665), (11, 0.034287197030052854), (12, 0.021483869200653788), (13, 0.033468696091416075), (14, 0.014070713898823948), (15, 0.029487668742227795), (16, 0.025213173521698867), (17, 0.11020027789932013)])
我期望的结果如下:
array([[(0, 0.022190866145025672),
(1, 0.03713307553569147),
(2, 0.03329292095418187),
(3, 0.013397487788645558),
(4, 0.012968754425823006),
(5, 0.02303938132767878),
(6, 0.5055000245070915),
(7, 0.02148198409145118)],
[(0, 0.027141399289287292),
(1, 0.034094330130799384),
(2, 0.18793352530921031),
(3, 0.01545954116652308),
(4, 0.03658418672184262),
(5, 0.1929824217980975),
(6, 0.021051003033754387),
(7, 0.03489076576637312)]])
作为进一步的信息,我需要数组中的嵌套列表来计算两个分布之间的Jesen-Shannon散度
嗨,我已经用传统的for循环方式完成了:
是的,您可以使用
list()
将元组转换为列表 方法输出
相关问题 更多 >
编程相关推荐