我有一個有效的代碼,但是我想學習一個更 Pythonic 的版本,因為我覺得我的代碼很笨拙。
想法是計算串列(“名稱”)中出現的次數并將其排序。
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])
uj5u.com熱心網友回復:
使用collections.Counter:
from collections import Counter
names = ["Jessica", "John", "Steve", "John", "John", "Steve"]
res = Counter(names).most_common()
print(res)
輸出
[('John', 3), ('Steve', 2), ('Jessica', 1)]
uj5u.com熱心網友回復:
以下是一些建議,按與您的代碼的相似性順序列出。第一個直接改編自您的代碼,并稍作修改;最后一個是我會怎么做,與 Dani Mesejo 的回答相同。這些提議利用:
dict索引total[name];dict方法total.get(name, default=0);dict方法total.setdefault(name, default=0);dict子類defaultdict;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)
uj5u.com熱心網友回復:
您可能需要:
sorted_total = dict(sorted(zip(total, map(total.get, total)), key=lambda x: x[1], reverse=True))
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/314586.html
上一篇:std::bind如何系結傳遞給std::sort的兩個不同型別引數的二元運算?
下一篇:如何對串列中的串列進行排序?
