我有两个列表,其中1个存储少数名人的信息,另一个列表存储与这些演员有关的奖励信息。
我试图解决的问题是将这两个列表组合成一个奖励信息成为属性的一个列表,该属性基本上是奖励列表。是的,这很容易实现。
for actor in actor_info:
for award in award_list:
if actor['personid'] == award['personid']:
if not actor.get('awards', False):
actor.update({'awards':[]})
actor['awards'].append(award)
但是如果你观察上面的代码,它会迭代len(actor_info) * len(award_list)
次,这不是一个优雅的解决方案。对于这个问题还有其他任何观点,执行周期要少得多。
注意: 的
为了更清楚地解释这个问题,我在下面描述了使用的数据结构。 actor_info和award_info列表中的每个元素本质上都是一个字典。
actor_info = []
d = {}
d['personid'] = 1210
d['firstname'] = 'Robert , Jr'
d['lastname'] = 'Downey'
d['birthplace'] = 'manhattan, NY'
d1 = {}
d1['personid'] = 2842
d1['firstname'] = 'Brad'
d1['lastname'] = 'Pitt'
d1['birthplace'] = 'Shawnee, OK'
d2 = {}
d2['personid'] = 361
d2['fname'] = 'Cate'
d2['lname'] = 'Blanchett'
d2['birthplace'] = 'Melbournce, Victoria'
d3 = {}
d3['personid'] = 261
d3['fname'] = 'Meg'
d3['lname'] = 'Ryan'
d3['birthplace'] = 'Melbournce, Victoria'
actor_info.append(d)
actor_info.append(d1)
actor_info.append(d2)
actor_info.append(d3)
奖励信息:
k = {}
k['year'] = '1992'
k['won'] = 'NO'
k['category'] = 'Best Actor'
k['name'] = 'Academy Award'
k['movie'] = 'Chaplin'
k['personid'] = 1210
k1 = {}
k1['year'] = '2008'
k1['won'] = 'NO'
k1['category'] = 'Best Actor'
k1['name'] = 'Academy Award'
k1['movie'] = 'Tropic thunder'
k1['personid'] = 1210
k2 = {}
k2['year'] = '2008'
k2['won'] = 'NO'
k2['category'] = 'Best Actor'
k2['name'] = 'Academy Award'
k2['movie'] = 'The Curious Case of Benjamin Button'
k2['personid'] = 2842
k3 = {}
k3['year'] = '1989'
k3['won'] = 'yes'
k3['category'] = 'Best supporting Actress'
k3['name'] = 'Academy award'
k2['movie'] = 'Aviator'
k3['personid'] = 361
award_list = []
award_list.append(k)
award_list.append(k1)
award_list.append(k2)
award_list.append(k3)
答案 0 :(得分:1)
首先,您应该从字典切换到collections.namedtuple
,这允许您以普通属性的形式访问数据。
无论如何,你可以通过事先制作一个查找表来避免二次迭代。
idToActor = {a['personid']:a for a in actor_info}
for award in award_list:
actor = idToActor[award['personid']]
actor.setdefault('awards',[]).append(award)
答案 1 :(得分:0)
首先,我将重构你如何将actor_info存储到这样的东西:
actor_info['personid'] = {'fname':'Cate',...,"awards":list()}
使用您当前的代码,可以使用以下代码实现:
actor_info = {}
for actor in [d,d1,d2,d3]:
actor_info[actor['personid']] = dict([(k,v) for (k,v) in actor.items() if k != 'personid'])
actor_info[actor['personid']]['awards'] = list()
现在给每个演员颁发奖项
for award in award_list:
if actor_info.has_key(award['personid']): #if an actor exists with the same personID
actor_info[award['personid']]['awards'].append(award) #append the award to their award list
这将在大约O(N)时间内运行。