I have a list with duplicate lines:
lst = ["abc", "abc", "omg", "what", "abc", "omg"]
and I would like to create:
lst = ["3 abc", "2 omg", "what"]
so basically count the duplicates, delete the duplicates and add the amount to the beginning of the line.
Here's 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 an easier way to get the desired result in python?
Chris source
share