Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count duplicates in list and assign the sum into list

Tags:

python

I have a list with duplicate strings:

lst = ["abc", "abc", "omg", "what", "abc", "omg"]

and I would like to produce:

lst = ["3 abc", "2 omg", "what"]

so basically count duplicates, remove duplicates and add the sum to the beginning of the string.

This is how I do it right now:

from collections import Counter
list2=[]
for i in lst:
  y = dict(Counter(i))
  have = list(accumulate(y.items())) # creating [("omg", 3), ...]

  for tpl in have: #
    join_list = []
    if tpl[1] > 1:
      join_list.append(str(tpl[1])+" "+tpl[0])
    else:
      join_list.append(tpl[0])
  list2.append(', '.join(join_list))

Is there a easier way to obtain the desired result in python?

like image 960
Chris Avatar asked Dec 25 '22 00:12

Chris


2 Answers

It seems you are needlessly complicating things. Here is a very Pythonic approach:

>>> import collections
>>> class OrderedCounter(collections.Counter, collections.OrderedDict):
...   pass
... 
>>> lst = ["abc", "abc", "omg", "what", "abc", "omg"]
>>> counts = OrderedCounter(lst)
>>> counts
OrderedCounter({'abc': 3, 'omg': 2, 'what': 1})
>>> ["{} {}".format(v,k) if v > 1 else k for k,v in counts.items()]
['3 abc', '2 omg', 'what']
>>> 
like image 142
juanpa.arrivillaga Avatar answered Dec 26 '22 13:12

juanpa.arrivillaga


You've properly used the Counter type to accumulate the needed values. Now, it's just a matter of a more Pythonic way to generate the results. Most of all, pull the initialization out of the loop, or you'll lose all but the last entry.

list2 = []
for tpl in have:
    count = "" if tpl[1] == 0 else str(tpl[1])+" "
    list2.append(count + tpl[0])

Now, to throw all of that into a list comprehension:

list2 = [ ("" if tpl[1] == 0 else str(tpl[1])+" ") + tpl[0] \
          for tpl in have]
like image 38
Prune Avatar answered Dec 26 '22 13:12

Prune