字典变成矩阵python_在python中将字典转换为numpy矩阵开始吧。我现在不担⼼速度(Ipython和python3.4)In [473]: dd = {'Clinton': [{'ideology': -0.5, 'vote':80}, {'ideology': -0.75,
'vote':90},
{'ideology': -0.89, 'vote': 99},
{'ideology': -0.5, 'vote':80, 'review': "She is a presidential candidate"}],
'Alexander': [{'ideology': -0.1, 'vote':50}, {'ideology': -0.95, 'vote':20},
{'ideology': -0.19, 'vote': 19}, {'ideology': -0.2, 'vote':30, 'review': "Good"}]}
...
In [475]: dd
Out[475]:
{'Alexander': [{'ideology': -0.1, 'vote': 50},
{'ideology': -0.95, 'vote': 20},
{'ideology': -0.19, 'vote': 19},
{'ideology': -0.2, 'vote': 30, 'review': 'Good'}],
'Clinton': [{'ideology': -0.5, 'vote': 80},
{'ideology': -0.75, 'vote': 90},
{'ideology': -0.89, 'vote': 99},
{'ideology': -0.5, 'vote': 80, 'review': 'She is a presidential candidate'}]}
In [476]: dd.keys()
Out[476]: dict_keys(['Alexander', 'Clinton'])
In [478]: dd.values()
Out[478]: dict_values([[{'ideology': -0.1, 'vote': 50}, {'ideology': -0.95, 'vote': 20}, {'ideology':....}]])
...
为了⽣成⼀个记录数组,我需要⼀个元组的列表,每个元组都有⼀个字段的值。具有键值对的第⼀个记录。但价值是⼀个列表。在
(这些值列表显然是使⽤带有列表附加的默认字典的结果。这是构建字典的好⽅法,但不幸的是,对于数组,我们必须解压它。)
^{pr2}$
Better-包含3个字段的元组列表列表:In [483]: [[(k,vv['ideology'],vv['vote']) for vv in v] for k,v in dd.items()]
Out[483]:
[[('Alexander', -0.1, 50),
('Alexander', -0.95, 20),
('Alexander', -0.19, 19),
('Alexander', -0.2, 30)],
[('Clinton', -0.5, 80),
('Clinton', -0.75, 90),
('Clinton', -0.5, 80)]]
添加可能丢失的review字段In [484]: [[(k,vv['ideology'],vv['vote'],vv.get('review','')) for vv in v] for k,v in dd.items()] Out[484]:
[[('Alexander', -0.1, 50, ''),
('Alexander', -0.95, 20, ''),
('Alexander', -0.19, 19, ''),
('Alexander', -0.2, 30, 'Good')],
[('Clinton', -0.5, 80, ''),
('Clinton', -0.75, 90, ''),
('Clinton', -0.89, 99, ''),
python 定义数组
('Clinton', -0.5, 80, 'She is a presidential candidate')]]
In [485]: ll=[[(k,vv['ideology'],vv['vote'],vv.get('review','')) for vv in v] for k,v in dd.items()]
要在列表中展开列表,请使⽤intertools链In [486]: from itertools import chain
...
In [488]: list(chain(*ll))
Out[488]:
[('Alexander', -0.1, 50, ''),
('Alexander', -0.95, 20, ''),
('Alexander', -0.19, 19, ''),
('Alexander', -0.2, 30, 'Good'),
('Clinton', -0.5, 80, ''),
('Clinton', -0.75, 90, ''),
('Clinton', -0.89, 99, ''),
('Clinton', -0.5, 80, 'She is a presidential candidate')]
In [489]: ll1=list(chain(*ll))
...
定义数据类型:In [491]: dt=np.dtype([('name','U10'),('ideology',float),('vote',int),('review','U100')])
In [492]: data=np.array(ll1,dt)
In [493]: data
Out[493]:
array([('Alexander', -0.1, 50, ''), ('Alexander', -0.95, 20, ''),
('Alexander', -0.19, 19, ''), ('Alexander', -0.2, 30, 'Good'),
('Clinton', -0.5, 80, ''), ('Clinton', -0.75, 90, ''),
('Clinton', -0.5, 80, 'She is a presidential candidate')],
dtype=[('name', '
看起来不错。在最后⼀个数组创建步骤中没有迭代。在将字典转换为元组列表时有⼀个迭代。但在使⽤字典时,这种迭代是不可避免的。在

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。