Read duplicates in the list and assign the amount to the list

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?

+4
source share
5 answers

You seem to unnecessarily complicate 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']
>>> 
+7
source

You used the Counter type correctly to accumulate the required values. Now this is just a matter of a more Putin way of generating results. Most of all, get the initialization out of the loop or you will lose everything except the last record.

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

, :

list2 = [ ("" if tpl[1] == 0 else str(tpl[1])+" ") + tpl[0] \
          for tpl in have]
+1

:

lst = ["abc", "abc", "omg", "what", "abc", "omg"]
l = [lst.count(i) for i in lst] # Count number of duplicates
d = dict(zip(lst, l)) # Convert to dictionary
lst = [str(d[i])+' '+i if d[i]>1 else i for i  in d] # Convert to list of strings
+1

, ...

import operator

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

#dictionary
countDic = {}

#iterate lst to populate dictionary: {'what': 1, 'abc': 3, 'omg': 2}
for i in lst:
    if i in countDic:
        countDic[i] += 1
    else:
        countDic[i] = 1

#clean list
lst = []

#convert dictionary to an inverse list sorted by value: [('abc', 3), ('omg', 2), ('what', 1)]
sortedLst = sorted(countDic.items(), key=operator.itemgetter(0))

#iterate sorted list to populate list
for k in sortedLst:
    if k[1] != 1:
        lst.append(str(k[1]) + " " + k[0])
    else:
        lst.append(k[0])

#result
print lst

:

['3 abc', '2 omg', 'what']
+1
source

This is the only pythonic way to do this, and it is also fast.

import collections

lst = ["abc", "abc", "omg", "what", "abc", "omg"]
duplicates = collections.Counter(lst)

lst = [f"{value} {key}"
       if value > 1 else key
       for (key, value) in duplicates.items()]

Note: this code only works with Python 3.6+ due to f-string syntax in list comprehension.

0
source

Source: https://habr.com/ru/post/1657427/


All Articles