[英]Python3: Count the occurrences in a list and sort it descendant
I have a working code but, I am looking to learn a more pythonic version as I feel my code is very clunky.我有一个可用的代码,但是我希望学习更多 pythonic 版本,因为我觉得我的代码非常笨拙。
The ideia is to count the number of occurrences in a list ("Names") and sort it descendant. ideia 是计算列表(“名称”)中出现的次数并将其排序为后代。
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])
Use collections.Counter
:使用
collections.Counter
:
from collections import Counter
names = ["Jessica", "John", "Steve", "John", "John", "Steve"]
res = Counter(names).most_common()
print(res)
Output Output
[('John', 3), ('Steve', 2), ('Jessica', 1)]
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.
最后一个是我会怎么做,与 Dani Mesejo 的回答相同。 These propositions make use of:
这些提议利用了:
dict
indexing total[name]
; dict
索引total[name]
;dict
method total.get(name, default=0)
; dict
方法total.get(name, default=0)
;dict
method total.setdefault(name, default=0)
; dict
方法total.setdefault(name, default=0)
;dict
subclass defaultdict
; dict
子类defaultdict
;dict
subclass Counter
. dict
子类Counter
。# SIMPLE LOOP WITH .COUNT
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])
# SIMPLE LOOP WITH .COUNT, OPTIMIZED
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])
# DICT COMPREHENSION WITH .COUNT
names = ["Jessica", "John", "Steve", "John", "John", "Steve"]
total = {name: names.count(name) for name in names}
sorted_total = sorted(total, key=total.get, reverse=True)
for r in sorted_total:
print(r, total[r])
# DICT COMPREHENSION WITH .COUNT, OPTIMIZED
names = ["Jessica", "John", "Steve", "John", "John", "Steve"]
total = {name: names.count(name) for name in set(names)} # using set avoids recomputing the expensive .count more than once per unique name
sorted_total = sorted(total, key=total.get, reverse=True)
for r in sorted_total:
print(r, total[r])
# SIMPLE LOOP WITHOUT .COUNT
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])
# LOOP WITH .GET()
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])
# LOOP WITH .SETDEFAULT
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])
# LOOP WITH DEFAULTDICT
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])
# COUNTER
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)
You might need:您可能需要:
sorted_total = dict(sorted(zip(total, map(total.get, total)), key=lambda x: x[1], reverse=True))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.