Home > Software design >  Python3: Count the occurrences in a list and sort it descendant
Python3: Count the occurrences in a list and sort it descendant

Time:10-08

I have a working code but, I am looking to learn a more pythonic version as I feel my code is very clunky.

The ideia is to count the number of occurrences in a list ("Names") and sort it descendant.

aux_list = []
names = ["Jessica", "John", "Steve", "John", "John", "Steve"]
total = {}

for name in names:
    
    if name is not aux_list:
        aux_list.append(name)
        
    count = names.count(name)
    key = { name : count }
    total.update(key)

# sort desc     
sorted_total = sorted(total, key=total.get, reverse=True)

# Desired output: <NAME> <TOTAL> DESC ORDER
# John 3
# Steve 2
# Jessica 1

for r in sorted_total:
    print(r, total[r])

CodePudding user response:

Use collections.Counter:

from collections import Counter

names = ["Jessica", "John", "Steve", "John", "John", "Steve"]
res = Counter(names).most_common()
print(res)

Output

[('John', 3), ('Steve', 2), ('Jessica', 1)]

CodePudding user response:

You might need:

sorted_total = dict(sorted(zip(total, map(total.get, total)), key=lambda x: x[1], reverse=True))

CodePudding user response:

Here are a few propositions, listed in order of similarity with your code. The first one is directly adapted from your code with minor corrections; the last one is how I would do it, and is identical to Dani Mesejo's answer. These propositions make use of:

names = ["Jessica", "John", "Steve", "John", "John", "Steve"]
total = {}
    
for name in names:
    total[name] = names.count(name)
    
sorted_total = sorted(total, key=total.get, reverse=True)
    
for r in sorted_total:
    print(r, total[r])
names = ["Jessica", "John", "Steve", "John", "John", "Steve"]
total = {}
    
for name in names:
    if name not in total:     # adding this if avoids recomputing the expensive `.count`
        total[name] = names.count(name)
    
sorted_total = sorted(total, key=total.get, reverse=True)
    
for r in sorted_total:
    print(r, total[r])
names = ["Jessica", "John", "Steve", "John", "John", "Steve"]
total = {}
    
for name in names:
    if name not in total:
        total[name] = 0
    total[name]  = 1
    
sorted_total = sorted(total, key=total.get, reverse=True)
    
for r in sorted_total:
    print(r, total[r])
names = ["Jessica", "John", "Steve", "John", "John", "Steve"]
total = {}
    
for name in names:
    total[name] = total.get(name, 0)   1
    
sorted_total = sorted(total, key=total.get, reverse=True)
    
for r in sorted_total:
    print(r, total[r])
names = ["Jessica", "John", "Steve", "John", "John", "Steve"]
total = {}
    
for name in names:
    total.setdefault(name,0)
    total[name]  = 1
    
sorted_total = sorted(total, key=total.get, reverse=True)
    
for r in sorted_total:
    print(r, total[r])
import collections # defaultdict

names = ["Jessica", "John", "Steve", "John", "John", "Steve"]
total = collections.defaultdict(int)
    
for name in names:
    total[name]  = 1
    
sorted_total = sorted(total, key=total.get, reverse=True)
    
for r in sorted_total:
    print(r, total[r])
import collections # Counter

names = ["Jessica", "John", "Steve", "John", "John", "Steve"]
total = collections.Counter(names)
    
sorted_total = total.most_common()
    
for name,nb in sorted_total:
    print(name, nb)
  • Related