簡體   English   中英

創建唯一詞典列表的最佳方法

[英]Optimal Method of create unique list of dicts

所以我需要一種在python中創建字典列表的最佳方法。

所以我有一個看起來像這樣的列表:

[
   {'name': 'John', 'hobbies': ['Reading', 'Swimming']},
   {'name': 'Gina', 'hobbies': ['Skating', 'Cooking']},
   {'name': 'John', 'hobbies': ['Gardening', 'Swimming']}
]

所以我需要輸出是這樣的:

[
   {'name': 'John', 'hobbies': ['Reading', 'Swimming', 'Gardening']},
   {'name': 'Gina', 'hobbies': ['Skating', 'Cooking']},
]

如您所見,我需要為每個名稱創建一組興趣愛好,並確實創建一個唯一的字典列表。

這是我嘗試過的:

{v['_id']['route']: v for v in routes_list}.values()

但這並不需要創建一個集合

任何人都可以以最佳方式幫助我嗎?

謝謝。

如果您同意將輸出的結構從名稱更改為興趣愛好集而只是一個字典,則可以在線性時間內完成(忽略邊緣情況,即很多哈希沖突):

from collections import defaultdict

data = [
    {'name': 'John', 'hobbies': ['Reading', 'Swimming']},
    {'name': 'Gina', 'hobbies': ['Skating', 'Cooking']},
    {'name': 'John', 'hobbies': ['Gardening', 'Swimming']}
]

output = defaultdict(set)

for d in data:
    output[d['name']].update(d['hobbies'])

print(output)
# defaultdict(<class 'set'>, {'John': {'Reading', 'Swimming', 'Gardening'},
#                             'Gina': {'Cooking', 'Skating'}})

如果您堅持使用字典列表,那么您仍然可以實現幾乎線性的時間(列表查找仍然是O(n)),但是具有將索引映射到名稱的邏輯:

data = [
        {'name': 'John', 'hobbies': ['Reading', 'Swimming']},
        {'name': 'Gina', 'hobbies': ['Skating', 'Cooking']},
        {'name': 'John', 'hobbies': ['Gardening', 'Swimming']}
    ]

output = []
names_to_indices = {}
for d in data:
    if d['name'] not in names_to_indices:
        output.append({'name': d['name'], 'hobbies': d['hobbies']})
        names_to_indices[d['name']] = len(output) - 1
    else:
        index = names_to_indices[d['name']]
        for hobbie in d['hobbies']:
            if hobbie not in output[index]['hobbies']:
                output[index]['hobbies'].append(hobbie)
print(output)
# [{'name': 'John', 'hobbies': ['Reading', 'Swimming', 'Gardening']},
#  {'name': 'Gina', 'hobbies': ['Skating', 'Cooking']}]

如果您同意將業余愛好定為一組,則可以將其設為真正的線性時間(再次,如果我們忽略過多的哈希沖突的可能性):

data = [
        {'name': 'John', 'hobbies': ['Reading', 'Swimming']},
        {'name': 'Gina', 'hobbies': ['Skating', 'Cooking']},
        {'name': 'John', 'hobbies': ['Gardening', 'Swimming']}
    ]

output = []
names_to_indices = {}
for d in data:
    if d['name'] not in names_to_indices:
        output.append({'name': d['name'], 'hobbies': set(d['hobbies'])})
        names_to_indices[d['name']] = len(output) - 1
    else:
        index = names_to_indices[d['name']]
        output[index]['hobbies'].update(d['hobbies'])
print(output)
# [{'name': 'John', 'hobbies': {'Gardening', 'Swimming', 'Reading'}},
#  {'name': 'Gina', 'hobbies': {'Skating', 'Cooking'}}]

只需構建一個中間默認字典,即可使您在線性時間內完成此操作。 最后轉換回所需的結構。

inp = [
   {'name': 'John', 'hobbies': ['Reading', 'Swimming']},
   {'name': 'Gina', 'hobbies': ['Skating', 'Cooking']},
   {'name': 'John', 'hobbies': ['Gardening', 'Swimming']}
]

from collections import defaultdict
temp = defaultdict(set)
for d in inp:
    temp[d['name']].update(d['hobbies'])

result = [{'name':k, 'hobbies': list(v)} for k, v in temp.items()]

輸出:

[{'name': 'John', 'hobbies': ['Gardening', 'Reading', 'Swimming']},
 {'name': 'Gina', 'hobbies': ['Cooking', 'Skating']}]

暫無
暫無

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

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