Merge tuples with the same key

断了今生、忘了曾经 提交于 2019-12-06 18:57:43

问题


How to merge a tuple with the same key

list_1 = [("AAA", [123]), ("AAA", [456]), ("AAW", [147]), ("AAW", [124])]

and turn them into

list_2 = [("AAA", [123, 456]), ("AAW", [147, 124])]

回答1:


The most performant approach is to use a collections.defaultdict dictionary to store data as an expanding list, then convert back to tuple/list if needed:

import collections

list_1 = [("AAA", [123]), ("AAA", [456]), ("AAW", [147]), ("AAW", [124])]

c = collections.defaultdict(list)
for a,b in list_1:
    c[a].extend(b)  # add to existing list or create a new one

list_2 = list(c.items())

result:

[('AAW', [147, 124]), ('AAA', [123, 456])]

note that the converted data is probably better left as dictionary. Converting to list again loses the "key" feature of the dictionary.

On the other hand, if you want to retain the order of the "keys" of the original list of tuples, unless you're using python 3.6/3.7, you'd have to create a list with the original "keys" (ordered, unique), then rebuild the list from the dictionary. Or use an OrderedDict but then you cannot use defaultdict (or use a recipe)




回答2:


You can use a dict to keep track of the indices of each key to keep the time complexity O(n):

list_1 = [("AAA", [123]), ("AAA", [456]), ("AAW", [147]), ("AAW", [124])]
list_2 = []
i = {}
for k, s in list_1:
    if k not in i:
        list_2.append((k, s))
        i[k] = len(i)
    else:
        list_2[i[k]][1].extend(s)

list_2 would become:

[('AAA', [123, 456]), ('AAW', [147, 124])]



回答3:


You can create a dictionary and loop through the list. If the item present in dictionary append the value to already existing list else assign the value to key.

dict_1 = {}
for item in list_1:
    if item[0] in dict_1:
        dict_1[item[0]].append(item[1][0])
    else:
        dict_1[item[0]] = item[1]
list_2 = list(dict_1.items())



回答4:


Similarly to other answers, you can use a dictionary to associate each key with a list of values. This is implemented in the function merge_by_keys in the code snippet below.

import pprint

list_1 = [("AAA", [123]), ("AAA", [456]), ("AAW", [147]), ("AAW", [124])]

def merge_by_key(ts):

    d = {}
    for t in ts:
        key = t[0]
        values = t[1]
        if key not in d:
            d[key] = values[:]
        else:
            d[key].extend(values)

    return d.items()



result = merge_by_key(list_1)

pprint.pprint(result)


来源:https://stackoverflow.com/questions/52454582/merge-tuples-with-the-same-key

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!