python - 是否可以将嵌套的元组列表转换为 numpy 数组?
问题描述
我有一个嵌套的元组列表:
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 Divergence。
解决方案
嗨,我已经以传统的 for 循环方式完成了此操作:
是的,您可以使用list()
方法将元组转换为列表
tuple_lists =[[(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)]]
temp=[]
tuple_into_list=[]
for tuple_list in tuple_lists: # looping through list of elements and those elements contains tuples
for tuple_item in tuple_list:
temp.append(list(tuple_item)) #converting tuple into list and adding it into a list object
tuple_into_list.append(temp) #finally adding a temp object which is a collection of converted tuple into list
temp=[] #make it empty so in another iteration the converted tuple into list of elements will be there
tuple_into_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]]]
推荐阅读
- python - 保存的model.h5转换为tensorflow文件格式
- c# - 将对象/字符串列表转换为以逗号分隔的子字符串
- ios - CATextLayer 无法在 swift 中与 UILabel 文本水平对齐
- javascript - 将表中的项目(行)添加到另一个表行
- r - 使用 R 指定 row.name 打开 CSV 文件时出错
- python-3.x - 如何在第一个循环实例中跳过前三个元素
- java - 我正在使用 java 和 Aws dynamo db 研究 Alexa 技能。每次我输入数据时,它都会发送一个用户 ID 作为主键,我可以删除它吗
- c# - C#如何使用代码中的按钮单击功能,而不仅仅是用户单击
- python - 获取多表计数的 Python 脚本
- python - TypeError: ("'list' object is not callable", '发生在索引 0')