2017-03-01 76 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 

結果應該是一樣的。

參考: