2017-03-01 81 views
0

这可能很简单,大多数python用户。我有一个清单:建立一个Python字典与字典...从列表

adv1 = [9999, 'Group1', 12345, 'team1'] 
adv2 = [8888, 'Group2', 12341, 'team2'] 
adv3 = [8888, 'Group2', 46563, 'team3'] 
adv4 = [8888, 'Group2', 23478, 'team4'] 

all_adv = [adv1, adv2, adv3, adv4] # <- list of lists 

在数据中有2个“组”和4个“组”。我试图通过all_adv数据迭代创建一个字典八九不离十这样的:

{ 
    Group1 : 9999, 
    teams: { 
    team1 : 12345 
    }, 
    { 
    Group2 : 8888, 
    teams: { 
    team2 : 12341, 
    team3 : 46563, 
    team4 : 23478 
    } 
    } 

其在各自集团“团队”项下组中的所有球队。我无法弄清楚逻辑。我想要做这样的事情:

dict = {} 
dict['teams'] = [] 
for row in all_adv: 
    dict[row[1]] = row[0] 
    if row[1] not in dict: 
    dict['teams'] = [] 
    dict['teams'].append({row[3] : row[2]}) 

print dict 

输出:

{'Group2': 8888, 'Group1': 9999, 'teams': [{'team1': 12345}, {'team2': 12341}, {'team3': 46563}, {'team4': 23478}]} 

我不知道,但我想我需要做出个别团体的字典的名单,其中包括球队的字典。任何指针?

+0

你描述的不是一个语法正确的字典。在猜测,@ TigerhawkT3的答案可能是你想要的,如果你想把团队联系到团体 – aydow

回答

3

我推荐一本词典(而不是词典列表),其中包含组名和其他词典的值,包含组的ID和团队。

adv1 = [9999, 'Group1', 12345, 'team1'] 
adv2 = [8888, 'Group2', 12341, 'team2'] 
adv3 = [8888, 'Group2', 46563, 'team3'] 
adv4 = [8888, 'Group2', 23478, 'team4'] 

all_adv = [adv1, adv2, adv3, adv4] 

d = {} 
for i, n, s, t in all_adv: 
    if n not in d: 
     d[n] = {'id':i, 'teams':{}} 
    d[n]['teams'][t] = s 

结果:

>>> import pprint 
>>> pprint.pprint(d, width=30) 
{'Group1': {'id': 9999, 
      'teams': {'team1': 12345}}, 
'Group2': {'id': 8888, 
      'teams': {'team2': 12341, 
         'team3': 46563, 
         'team4': 23478}}} 
+0

非常感谢,这是伟大的。这是一个更好的设置。 – kevingduck

1

这可能也是一个解决办法:

from itertools import groupby 
from operator import itemgetter 


adv1 = [9999, 'Group1', 12345, 'team1'] 
adv2 = [8888, 'Group2', 12341, 'team2'] 
adv3 = [8888, 'Group2', 46563, 'team3'] 
adv4 = [8888, 'Group2', 23478, 'team4'] 
all_adv = [adv1, adv2, adv3, adv4] 

group_id_map = {ii[1]: ii[0] for ii in all_adv} 
all_adv.sort(key=itemgetter(1)) 
groups = {} 
for k, r in groupby(all_adv, key=itemgetter(1)): 
    teams = {ii[3]: ii[2] for ii in r} 
    group = dict(id=group_id_map[k], team=teams) 
    groups[k] = group 

而结果:

import json 


print(json.dumps(groups, indent=4)) 

{ 
    "Group1": { 
     "id": 9999, 
     "team": { 
      "team1": 12345 
     } 
    }, 
    "Group2": { 
     "id": 8888, 
     "team": { 
      "team2": 12341, 
      "team3": 46563, 
      "team4": 23478 
     } 
    } 
} 

如果我可以决定ADV1的类型, adv2 ...,我将使用namedtuple而不是列表beca使用它更容易使用。

from collections import namedtuple 
from itertools import groupby 
from operator import attrgetter 


Team = namedtuple('Team', 'group_id group_name team_id team_name') 

adv1 = [9999, 'Group1', 12345, 'team1'] 
adv2 = [8888, 'Group2', 12341, 'team2'] 
adv3 = [8888, 'Group2', 46563, 'team3'] 
adv4 = [8888, 'Group2', 23478, 'team4'] 
all_adv = [adv1, adv2, adv3, adv4] 
all_adv = [Team(*ii) for ii in all_adv] 

group_id_map = {ii.group_name: ii.group_id for ii in all_adv} 
all_adv.sort(key=attrgetter('group_name')) 
groups = {} 
for k, r in groupby(all_adv, key=attrgetter('group_name')): 
    teams = {ii.team_name: ii.team_id for ii in r} 
    group = dict(id=group_id_map[k], team=teams) 
    groups[k] = group 

结果应该是一样的。

参考: