2015-12-02 48 views
0

我仍然試圖找出嵌套在python中的字典是如何工作的。Python中的嵌套字典:如何使它們以及如何使用它們?

我知道,當你使用[]它是一個列表,()它是一個元組和{}字典。 但是,當你想嵌套dictionnaries這樣的結構(這是什麼。我想):

{KeyA : 
     {ValueA : 
       [KeyB : ValueB], 
       [Keyc : ValueC], 
       [KeyD : ValueD]}, 
           {ValueA for each ValueD]}} 

現在我有一個像字典:

{KeyA : {KeyB : [ValueB], 
     KeyC : [ValueC], 
     KeyD : [ValueD]}} 

這是我的代碼

json_file = importation() 
    dict_guy = {}           
    for key, value in json_file['clients'].items(): 
     n_customerID = normalization(value['shortname']) 
     if n_customerID not in dict_guy: 
      dict_guy[n_customerID] = { 
       'clientsName':[], 
       'company':[], 
       'contacts':[], } 
     dict_guy[n_customerID]['clientsName'].append(n_customerID) 
     dict_guy[n_customerID]['company'].append(normalization(value['name'])) 
     dict_guy[n_customerID]['contacts'].extend([norma_email(item) for item in v\ 
alue['contacts']]) 

有人可以請給我更多的信息或真正向我解釋嵌套字典是如何工作的? 謝謝

+0

請解釋'{ValueA for each ValueD]}應該是什麼意思,因爲它不是一個有效的Python代碼。 – Maciek

+0

@Maciek這意味着我需要通過參考dict_guy ['n_customerID'] ['contacts']中提供的聯繫人來報告客戶端名稱。對於現場聯繫我需要報告客戶端名稱相關聯。你有更好的理解,還是不清楚? – CRC

+0

對不起,還沒有得到它:(也許寫一個你想如何使用你的嵌套字典的例子?像'print(dict_guy [1234] ['contacts'] ['[email protected]'] [''姓名'])'? – Maciek

回答

1

所以,我希望我得到它的權利,從我們的談話在評論:)

json_file = importation() 
dict_guy = {}           
for key, value in json_file['clients'].items(): 
    n_customerID = normalization(value['shortname']) 
    if n_customerID not in dict_guy: 
     dict_guy[n_customerID] = { 
      'clientsName':[], 
      'company':[], 
      'contacts':{}, } # Assign empty dict, not list 
    dict_guy[n_customerID]['clientsName'].append(n_customerID) 
    dict_guy[n_customerID]['company'].append(normalization(value['name'])) 
    for item in value['contacts']: 
     normalized_email = norma_email(item) 
     # Use the contacts dictionary like every other dictionary 
     dict_guy[n_customerID]['contacts'][normalized_email] = n_customerID 

沒有簡單地分配一個字典到另一個字典裏面的一個關鍵沒有問題。這就是我在這個代碼示例中所做的。您可以根據需要創建嵌套深度的字典。

這是如何幫助你。如果沒有,我們會繼續努力:)

編輯: 關於列表/字典的解釋。您是幾乎正確的:

我知道,當你使用[]這是一個列表,(),它是一個元組和{}的字典。

{}括號在Python 3中有點棘手。它們可以用來創建字典和集合!

a = {} # a becomes an empty dictionary 
a = set() # a becomes an empty set 
a = {1,2,3} # a becomes a set with 3 values 
a = {1: 1, 2: 4, 3: 9} # a becomes a dictionary with 3 keys 
a = {x for x in range(10)} # a becomes a set with 10 elements 
a = {x: x*x for x in range(10)} # a becomes a dictionary with 10 keys 

你行dict_guy[n_customerID] = { {'clientsName':[], 'company':[], 'contacts':[]}}試圖建立一個與單個字典中把它設置因爲字典是不是哈希的,你得到了TypeError例外通知您自己是不是哈希的:)(套只能存儲對此語句這是可散列的)

+0

哦,我明白了!我在說話的時候遇到了錯誤,我嘗試了一些改變,並且發生了錯誤TypeError:unashable type:'dict',但現在我明白了爲什麼!在這裏我的錯誤代碼:'如果n_customerID不在dict_guy: dict_guy [n_customerID] = { {'clientsName':[], 'company':[], 'contacts':[]}}'你的代碼吧!比你 – CRC

+0

好吧...它的作品!非常感謝你,你只是回答我所有關於嵌套詞典是如何工作的問題,我想知道如何獲得這個結果,最後你開啓了我的燈籠(不知道我是否可以用英語說,這是一個法語表達)。我還有一個問題:我怎樣才能看到我的字典中有多少級別,以確保它是正確的? – CRC

+1

調試嵌套結構的最佳方式是使用漂亮的打印 - 一種顯示字典和其他具有良好佈局的東西的方法。只需'輸入pprint'和'pprint.pprint(the_dictionary)'。更多關於它的地方在這裏:https://docs.python.org/3.4/library/pprint.html#pprint.pprint – Maciek

相關問題