簡體   English   中英

從列表列表創建字典

[英]Create dictionary from list of list

我想從列表數據列表中創建如下字典。 我更喜歡單線。 謝謝。

data = [['49', '69'], ['51', '1'], ['53', '1'], ['53', '4'], ['53', '132'], ['54', '209'], ['56', '1'], ['56', '202'], ['56', '203'], ['56', '204'], ['56', '205'], ['56', '206'], ['56', '207'], ['56', '208'], ['56', '209'], ['56', '210'], ['56', '211'], ['56', '212'], ['56', '213'], ['56', '214'], ['56', '215'], ['56', '216'], ['56', '217'], ['58', '1'], ['58', '10'], ['59', '1'], ['59', '9'], ['60', '8'], ['61', '1'], ['61', '5'], ['61', '8'], ['62', '1'], ['62', '9'], ['62', '40'], ['62', '41'], ['62', '42'], ['63', '177'], ['64', '1'], ['64', '9'], ['64', '141'], ['65', '155'], ['65', '156'], ['65', '157'], ['66', '1'], ['66', '194'], ['67', '1'], ['67', '10'], ['68', '1'], ['68', '9'], ['69', '1'], ['69', '10'], ['69', '139'], ['69', '140'], ['69', '141'], ['69', '142'], ['69', '143'], ['69', '144'], ['69', '145'], ['69', '146'], ['69', '147'], ['69', '148'], ['69', '149'], ['69', '150'], ['69', '151'], ['69', '152'], ['69', '153'], ['69', '154'], ['69', '155'], ['69', '156'], ['69', '157'], ['69', '158'], ['69', '159'], ['69', '160'], ['69', '161'], ['69', '162'], ['69', '163'], ['69', '164'], ['69', '165'], ['69', '166'], ['69', '167'], ['69', '168'], ['69', '169'], ['69', '170'], ['69', '171'], ['69', '172'], ['69', '173'], ['69', '174'], ['69', '175'], ['69', '176'], ['69', '177'], ['69', '178'], ['69', '179'], ['69', '180'], ['69', '181'], ['69', '182'], ['69', '183'], ['69', '184'], ['69', '185'], ['69', '186'], ['69', '187'], ['69', '188'], ['69', '189'], ['69', '190'], ['69', '191'], ['69', '192'], ['69', '193'], ['69', '194'], ['69', '195'], ['69', '196'], ['69', '197'], ['69', '198'], ['69', '199'], ['69', '200'], ['69', '201'], ['69', '202'], ['69', '203'], ['69', '204'], ['69', '205'], ['69', '206'], ['69', '207'], ['69', '208'], ['69', '209'], ['69', '210'], ['69', '211'], ['69', '212'], ['69', '213'], ['69', '214'], ['69', '215'], ['69', '216'], ['69', '217'], ['69', '218'], ['69', '219'], ['69', '220'], ['69', '221'], ['69', '222'], ['69', '223'], ['69', '224'], ['69', '225'], ['69', '226'], ['69', '227'], ['69', '228'], ['69', '229'], ['69', '230'], ['69', '231'], ['69', '232'], ['69', '233'], ['69', '234'], ['69', '235'], ['69', '236'], ['69', '237'], ['69', '238'], ['69', '239'], ['69', '240'], ['69', '241'], ['69', '242'], ['69', '243'], ['69', '244'], ['69', '245'], ['69', '246'], ['69', '247'], ['69', '248'], ['69', '249'], ['69', '250'], ['69', '251'], ['69', '252'], ['69', '253'], ['69', '254'], ['69', '255'], ['69', '256'], ['69', '257'], ['69', '258'], ['69', '259'], ['69', '260'], ['69', '261'], ['69', '262'], ['69', '263'], ['69', '264'], ['69', '265'], ['69', '266'], ['69', '267'], ['69', '268'], ['69', '269'], ['69', '270'], ['69', '271'], ['69', '278'], ['70', '1'], ['70', '9']]

dict = {49:[69], 51:[1], 53:[1,4,132] ......}

請不要使用dict作為變量名,因為它是一個內置函數。

dictionary = {}
for d in data:
    try:
        dictionary[int(d[0])].append(int(d[1]))
    except KeyError:
        dictionary[int(d[0])] = [int(d[1])]

使用 try/except 比 if/else 更優化,因為 python 解釋器在拋出錯誤和捕獲它們方面比評估表達式要快得多。

你可以這樣做。

d = {}
for i in data:
     if i[1] in d:
     d[i[1]].append(i[0])
    else:
        d[i[1]] = [i[0]]

希望能幫助到你 : )

這是defaultdict一個很好的用例:

from collections import defaultdict

my_dict = defaultdict(list) # don't shadow the name `dict`!

for key, value in data:
    my_dict[int(key)].append(int(value))
data = [['49', '69'], ['51', '1'], ['53', '1'], ['53', '4'], ['53', '132'], ['54', '209'], ['56', '1'], ['56', '202'], ['56', '203'], ['56', '204'], ['56', '205'], ['56', '206'], ['56', '207'], ['56', '208'], ['56', '209'], ['56', '210'], ['56', '211'], ['56', '212'], ['56', '213'], ['56', '214'], ['56', '215'], ['56', '216'], ['56', '217'], ['58', '1'], ['58', '10'], ['59', '1'], ['59', '9'], ['60', '8'], ['61', '1'], ['61', '5'], ['61', '8'], ['62', '1'], ['62', '9'], ['62', '40'], ['62', '41'], ['62', '42'], ['63', '177'], ['64', '1'], ['64', '9'], ['64', '141'], ['65', '155'], ['65', '156'], ['65', '157'], ['66', '1'], ['66', '194'], ['67', '1'], ['67', '10'], ['68', '1'], ['68', '9'], ['69', '1'], ['69', '10'], ['69', '139'], ['69', '140'], ['69', '141'], ['69', '142'], ['69', '143'], ['69', '144'], ['69', '145'], ['69', '146'], ['69', '147'], ['69', '148'], ['69', '149'], ['69', '150'], ['69', '151'], ['69', '152'], ['69', '153'], ['69', '154'], ['69', '155'], ['69', '156'], ['69', '157'], ['69', '158'], ['69', '159'], ['69', '160'], ['69', '161'], ['69', '162'], ['69', '163'], ['69', '164'], ['69', '165'], ['69', '166'], ['69', '167'], ['69', '168'], ['69', '169'], ['69', '170'], ['69', '171'], ['69', '172'], ['69', '173'], ['69', '174'], ['69', '175'], ['69', '176'], ['69', '177'], ['69', '178'], ['69', '179'], ['69', '180'], ['69', '181'], ['69', '182'], ['69', '183'], ['69', '184'], ['69', '185'], ['69', '186'], ['69', '187'], ['69', '188'], ['69', '189'], ['69', '190'], ['69', '191'], ['69', '192'], ['69', '193'], ['69', '194'], ['69', '195'], ['69', '196'], ['69', '197'], ['69', '198'], ['69', '199'], ['69', '200'], ['69', '201'], ['69', '202'], ['69', '203'], ['69', '204'], ['69', '205'], ['69', '206'], ['69', '207'], ['69', '208'], ['69', '209'], ['69', '210'], ['69', '211'], ['69', '212'], ['69', '213'], ['69', '214'], ['69', '215'], ['69', '216'], ['69', '217'], ['69', '218'], ['69', '219'], ['69', '220'], ['69', '221'], ['69', '222'], ['69', '223'], ['69', '224'], ['69', '225'], ['69', '226'], ['69', '227'], ['69', '228'], ['69', '229'], ['69', '230'], ['69', '231'], ['69', '232'], ['69', '233'], ['69', '234'], ['69', '235'], ['69', '236'], ['69', '237'], ['69', '238'], ['69', '239'], ['69', '240'], ['69', '241'], ['69', '242'], ['69', '243'], ['69', '244'], ['69', '245'], ['69', '246'], ['69', '247'], ['69', '248'], ['69', '249'], ['69', '250'], ['69', '251'], ['69', '252'], ['69', '253'], ['69', '254'], ['69', '255'], ['69', '256'], ['69', '257'], ['69', '258'], ['69', '259'], ['69', '260'], ['69', '261'], ['69', '262'], ['69', '263'], ['69', '264'], ['69', '265'], ['69', '266'], ['69', '267'], ['69', '268'], ['69', '269'], ['69', '270'], ['69', '271'], ['69', '278'], ['70', '1'], ['70', '9']]


o_dict = dict()
for ele in data:
    k,v = int(ele[0]), int(ele[1])
    if k in o_dict.keys():
        o_dict[k].append(v)
    else:
        o_dict[k] = [v]
print(o_dict)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM