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?
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']
>>>
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With