Home > Back-end >  What is the fastest way to combine two lists of tuples, merging duplicates of i[0], while adding i[1
What is the fastest way to combine two lists of tuples, merging duplicates of i[0], while adding i[1

Time:03-25

I'm combing lists of tuples that look like the following:

list1 = [('word', 3), ('example', 2), ('another', 1)]
list2 = [('and', 1), ('word', 4)]

I want to combine them so that I remove duplicates while adding the second value, the result here would look like:

result = [('word', 7), ('example', 2), ('another', 1), ('and', 1)]

The code I have to achieve this is this:

def combineTokenCount(list1, list2):
    tokenCount = {}
    list = list1   list2
    for word in list:
        if word[0] not in tokenCount:
             tokenCount[word[0]] = int(word[1])
        else:
             tokenCount[word[0]]  = int(word[1])
        tokenCount = [(k,v) for k,v in tokenCount.items()]
        tokenCount.sort(key = lambda x: x[1], reverse=True)
    return tokenCount

This works but is inefficient. Is there a better way to do this?

CodePudding user response:

Use collections.Counter.

>>> from collections import Counter
>>> c = Counter()
>>> c.update(dict(list1))
>>> c.update(dict(list2))
>>> c
Counter({'word': 7, 'example': 2, 'another': 1, 'and': 1})

or in one line,

c = Counter(dict(list1))   Counter(dict(list2))

Counter is a subclass of dict, but if you really want a plain dict in the end, you can do that with

d = dict(c)

or if you want a list of tuples,

result = list(c.items())

CodePudding user response:

Try:

>>> list({k: dict(list1).get(k,0) dict(list2).get(k,0) for k in dict(list1 list2)}.items())
[('word', 7), ('example', 2), ('another', 1), ('and', 1)]

CodePudding user response:

   from collections import Counter
 
   list1 = [('word', 3), ('example', 2), ('another', 1)]
   list2 = [('and', 1), ('word', 4)]

   def ourfunct(list1,list2):
      result=Counter(dict(list1)) Counter(dict(list2))
      final = list(dict(result).items())
      return(final)
   
   [('word', 7), ('example', 2), ('another', 1), ('and', 1)]
  • Related