2016-08-30 60 views
0

我有一本詞典,dict1,我想找到一種方法來循環查看牧羊人,大col和貴賓犬的所有值。如果我的語法關閉,我對一段時間的道歉表示歉意。我仍然在學習字典!循環訪問字典以製作多個字典

dict1 = { 
'Bob VS Sarah': { 
    'shepherd': 1, 
    'collie': 5, 
    'poodle': 8 
}, 
'Bob VS Ann': { 
    'shepherd': 3, 
    'collie': 2, 
    'poodle': 1 
}, 
'Bob VS Jen': { 
    'shepherd': 3, 
    'collie': 2, 
    'poodle': 2 
}, 
'Sarah VS Bob': { 
    'shepherd': 3, 
    'collie': 2, 
    'poodle': 4 
}, 
'Sarah VS Ann': { 
    'shepherd': 4, 
    'collie': 6, 
    'poodle': 3 
}, 
'Sarah VS Jen': { 
    'shepherd': 1, 
    'collie': 5, 
    'poodle': 8 
}, 
'Jen VS Bob': { 
    'shepherd': 4, 
    'collie': 8, 
    'poodle': 1 
}, 
'Jen VS Sarah': { 
    'shepherd': 7, 
    'collie': 9, 
    'poodle': 2 
}, 
'Jen VS Ann': { 
    'shepherd': 3, 
    'collie': 7, 
    'poodle': 2 
}, 
'Ann VS Bob': { 
    'shepherd': 6, 
    'collie': 2, 
    'poodle': 5 
}, 
'Ann VS Sarah': { 
    'shepherd': 0, 
    'collie': 2, 
    'poodle': 4 
}, 
'Ann VS Jen': { 
    'shepherd': 2, 
    'collie': 8, 
    'poodle': 2 
}, 
'Bob VS Bob': { 
    'shepherd': 3, 
    'collie': 2, 
    'poodle': 2 
}, 
'Sarah VS Sarah': { 
    'shepherd': 3, 
    'collie': 2, 
    'poodle': 2 
}, 
'Ann VS Ann': { 
    'shepherd': 13, 
    'collie': 2, 
    'poodle': 4 
}, 
'Jen VS Jen': { 
    'shepherd': 9, 
    'collie': 7, 
    'poodle': 2 
} 
} 

這就是我想要的東西,例如,但要能遍歷做出字典每隻狗:

dict_shepherd = {「牧羊人」:1,3,3,3-四氟丙烯, 4,1,4,7,3,6,0,2,3,3,13,9}

注意:我還沒有觸及熊貓基地,更喜歡幫助,而不使用它們:)我會得到他們有一天。

+2

一個問題,這種方法的字典是一個'dict'沒有訂單,所以你的列表可能與您寫的順序不一樣。訂單是否重要? –

回答

2

您可以在一般的情況下與defaultdict(list)解決它的子類型的字典鍵的可變數目:

from collections import defaultdict 
from pprint import pprint 

dict1 = # your dictionary of dictionaries here, removed to shorten the presented code 

d = defaultdict(list) 
for sub_dict in dict1.values(): 
    for key, value in sub_dict.items(): 
     d[key].append(value) 

pprint(dict(d)) 

將產生:

{'collie': [2, 7, 8, 5, 2, 6, 5, 2, 2, 2, 2, 8, 9, 2, 2, 7], 
'poodle': [2, 2, 1, 8, 2, 3, 8, 5, 4, 1, 2, 2, 2, 4, 4, 2], 
'shepherd': [3, 9, 4, 1, 3, 4, 1, 6, 3, 3, 3, 2, 7, 0, 13, 3]} 
+0

有沒有辦法制作單獨的字典而不是一個? –

2
dict_shepherd = {'shepherd': []} 
for name in dict1: 
    dict_shepherd['shepherd'].append(dict1['shepherd']) 

值得一提的是標準字典不執行其內容的任何順序,因此通過項目循環,因爲它們是在您的示例中列出可能不會產生它們以相同的順序。

2

你也可以得到所有的列表在一行中使用字典和列表解析:

ds = {type: [val[type] for val in dict1.values()] for type in ['shepherd', 'collie', 'poodle']} 

# {'collie': [2, 7, 8, 5, 2, 6, 5, 2, 2, 2, 2, 8, 9, 2, 2, 7], 
# 'poodle': [2, 2, 1, 8, 2, 3, 8, 5, 4, 1, 2, 2, 2, 4, 4, 2], 
# 'shepherd': [3, 9, 4, 1, 3, 4, 1, 6, 3, 3, 3, 2, 7, 0, 13, 3]} 

但是,列表是在n o特定訂單,因爲dict沒有訂單。

1

您可以通過用戶defaultdict如下

from collections import defaultdict 

dict1 = {'Bob VS Sarah': {'shepherd': 1,'collie': 5,'poodle': 8}, 
'Bob VS Ann': {'shepherd': 3,'collie': 2,'poodle': 1}, 
'Bob VS Jen': {'shepherd': 3,'collie': 2,'poodle': 2}, 
'Sarah VS Bob': {'shepherd': 3,'collie': 2,'poodle': 4}, 
'Sarah VS Ann': {'shepherd': 4,'collie': 6,'poodle': 3}, 
'Sarah VS Jen': {'shepherd': 1,'collie': 5,'poodle': 8}, 
'Jen VS Bob': {'shepherd': 4,'collie': 8,'poodle': 1}, 
'Jen VS Sarah': {'shepherd': 7,'collie': 9,'poodle': 2}, 
'Jen VS Ann': {'shepherd': 3,'collie': 7,'poodle': 2}, 
'Ann VS Bob': {'shepherd': 6,'collie': 2,'poodle': 5}, 
'Ann VS Sarah': {'shepherd': 0,'collie': 2,'poodle': 4}, 
'Ann VS Jen': {'shepherd': 2,'collie': 8,'poodle': 2}, 
'Bob VS Bob': {'shepherd': 3,'collie': 2,'poodle': 2}, 
'Sarah VS Sarah': {'shepherd': 3,'collie': 2,'poodle': 2}, 
'Ann VS Ann': {'shepherd': 13,'collie': 2,'poodle': 4}, 
'Jen VS Jen': {'shepherd': 9,'collie': 7,'poodle': 2}} 


def iter_dict(dict_, result=defaultdict(list)): # mutable as default value ot reuse result over the recursion 
    for k, v in dict_.items(): 
     if isinstance(v, dict): 
      iter_dict(v) 
     else: 
      result[k].append(v) 
    return result 

print(iter_dict(dict1)) 

這將產生所有預期成果

defaultdict(<class 'list'>, {'shepherd': [3, 4, 9, 2, 3, 1, 0, 4, 1, 6, 3, 3, 13, 3, 3, 7], 'collie': [2, 8, 7, 8, 7, 5, 2, 6, 5, 2, 2, 2, 2, 2, 2, 9], 'poodle': [1, 1, 2, 2, 2, 8, 4, 3, 8, 5, 2, 2, 4, 2, 4, 2]})