2016-02-22 46 views
0

我寫了簡單的幾行代碼來總結python字典的元素,使其成爲uniqe。Python總和字典元素(更好的pythonic方式)

test = [{'id': 3L, 'category': 'BOOK', 'information': 'abcdefghijk', 'name': 'test', 'sum': 5}, 
     {'id': 3L, 'category': 'BOOK', 'information': 'abcdefghijk', 'name': 'test', 'sum': 1}, 
     {'id': 3L, 'category': 'BOOK', 'information': 'abcdefghijk', 'name': 'test', 'sum': 3}, 
     {'id': 4L, 'category': 'BOOK', 'information': 'abcdefghijk', 'name': 'test2', 'sum': 1}, 
     {'id': 4L, 'category': 'BOOK', 'information': 'abcdefghijk', 'name': 'test2', 'sum': 1}, 
     {'id': 5L, 'category': 'BOOK', 'information': 'abcdefghijk', 'name': 'test4', 'sum': 1}, 
     {'id': 6L, 'category': 'BOOK', 'information': 'abcdefghijk', 'name': 'test5', 'sum': 1}] 

newlist = [] 
newdict = {} 

for dict_item in test: 

    pixel = dict_item['id'] 
    country = dict_item['category'] 
    ac = dict_item['information'] 
    name = dict_item['name'] 
    sum = dict_item['sum'] 

    if newdict.has_key(pixel): 

     pos = newdict[pixel] 
     newlist[pos] = {'id': pixel, 'category': country, 'information': ac, 'name': name, 'sum': (newlist[pos]['sum']+sum)} 
    else: 
     newlist.append(dict_item) 
     newdict[pixel] = len(newlist) -1 

print newlist 

我總和的結果:

[{'category': 'BOOK', 'information': 'abcdefghijk', 'sum': 9, 'id': 3L, 'name': 'test'},{'category': 'BOOK', 'information': 'abcdefghijk', 'sum': 2, 'id': 4L, 'name': 'test2'},{'category': 'BOOK', 'information': 'abcdefghijk', 'sum': 2, 'id': 5L, 'name': 'test5'}] 

有沒有更好的方式來總結這本字典來總結ID是唯一的?

+2

tbh我不明白dowvotes,這似乎是一個很好的問題給我。如果downvoters可以闡述,這將是建設性的。 – Lisa

+0

不要使用'has_key',使用'in'; 'has_key'在3.x中被棄用,而'in'則更具慣用。 – leongold

+0

具有相同'id'但具有不同'category','information','name',...的輸入的輸出數據究竟保留什麼? – dlask

回答

0
test = [{'id': 3937L, 'category': 'BOOK', 'information': 'abcdefghijk', 'name': 'test', 'sum': 5}, 
     {'id': 3937L, 'category': 'BOOK', 'information': 'abcdefghijk', 'name': 'test', 'sum': 1}, 
     {'id': 3937L, 'category': 'BOOK', 'information': 'abcdefghijk', 'name': 'test', 'sum': 3}, 
     {'id': 4215L, 'category': 'BOOK', 'information': 'abcdefghijk', 'name': 'test2', 'sum': 1}, 
     {'id': 4215L, 'category': 'BOOK', 'information': 'abcdefghijk', 'name': 'test2', 'sum': 1}, 
     {'id': 4217L, 'category': 'BOOK', 'information': 'abcdefghijk', 'name': 'test4', 'sum': 1}, 
     {'id': 4218L, 'category': 'BOOK', 'information': 'abcdefghijk', 'name': 'test5', 'sum': 1}] 

dct = {} 

for d in test: 
    if d["id"] in dct: 
     dct[d["id"]]["sum"] += d["sum"] 
    else: 
     dct[d["id"]] = d.copy() 

from pprint import pprint as pp 

pp(dct.values()) 

輸出相匹配運行自己的代碼:

[{'category': 'BOOK', 
    'id': 4217L, 
    'information': 'abcdefghijk', 
    'name': 'test4', 
    'sum': 1}, 
{'category': 'BOOK', 
    'id': 3937L, 
    'information': 'abcdefghijk', 
    'name': 'test', 
    'sum': 9}, 
{'category': 'BOOK', 
    'id': 4218L, 
    'information': 'abcdefghijk', 
    'name': 'test5', 
    'sum': 1}, 
{'category': 'BOOK', 
    'id': 4215L, 
    'information': 'abcdefghijk', 
    'name': 'test2', 
    'sum': 2}] 

你可以簡單地調用dict.copy()創造新類型的字典,你不需要手動創建一個新的字典各一次。

+0

Thx我喜歡你的答案 – moviss

+0

@moviss,不用擔心,當你想複製字典,如果你有其他可變對象作爲你需要'copy.deepcopy'的值時,dict.copy是非常高效的。 –

0

不需要重新創建整個字典及其所有的鍵和值。只需使用dict即可創建原始字典的副本,或添加到列表中現有字典的總和中。此外,你可以從newdict得到newlist

newdict = {} # or collections.OrderedDict() if order is important 
for dict_item in test: 
    pixel = dict_item['id'] 
    if pixel in newdict: 
     newdict[pixel]["sum"] += dict_item['sum'] 
    else: 
     newdict[pixel] = dict(dict_item) 
newlist = list(newdict.values()) 
+0

Thx爲答案:) – moviss

0

我會這樣做。基本上,結合所有具有相同ID的字典。我也堅持使用字典而不是列表,以便您輕鬆選擇從鍵到值的映射。

squashed = {} 
for inner in test: 
    key = inner['id'] 
    del inner['id'] 
    if key in squashed: 
     squashed[key]['sum'] += inner['sum'] 
    else: 
     squashed[key] = inner 
+0

這將修改原來的字典。不知道這是否是有意的。另外,爲什麼你刪除'id'字段? –

+0

好點!我刪除了ID字段,因爲它不必將ID存儲兩次,作爲密鑰並再次存儲在字典中。但是,對於OP來說,將它保留在字典中會更好/更容易。 –

+0

我不想刪除身份證。順便說一句,回答! – moviss