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?
Try this:
Another possible solution with comments to help...
Output:
This is the only Pythonic way of doing it, and it is also fast.
Note: this code only works with Python 3.6+ because of the f-string syntax in the list comprehension.
It seems you are needlessly complicating things. Here is a very Pythonic approach:
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.
Now, to throw all of that into a list comprehension: