这是一个开始。我现在不担心速度(Ipython with 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':....}]])
...
要创建一个记录数组,我需要一个元组列表,每个元组都有每个字段的值。具有键值对的第一条记录。但值是一个列表。
(这些值列表显然是使用带有列表追加的默认字典的结果。这是构建字典的好方法,但不幸的是,对于数组,我们必须解包它。)
In [480]: [(k,v) for k,v in dd.items()]
Out[480]:
[('Alexander',
[{'ideology': -0.1, 'vote': 50},
{'ideology': -0.95, 'vote': 20},
....
'review': 'She is a presidential candidate'}])]
更好 - 具有 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.89, 99),
('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, ''),
('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.89, 99, ''),
('Clinton', -0.5, 80, 'She is a presidential candidate')],
dtype=[('name', '<U10'), ('ideology', '<f8'), ('vote', '<i4'), ('review', '<U100')])
看起来不错。在最后一个数组创建步骤中没有迭代。将字典转换为元组列表时存在迭代。但在使用字典时,这种迭代是不可避免的。