📜  Python|按频率排序列表元素

📅  最后修改于: 2022-05-13 01:55:12.018000             🧑  作者: Mango

Python|按频率排序列表元素

给定一个包含重复和非重复元素的列表,任务是根据元素的频率对给定列表进行排序。让我们讨论一些相同的方法。

方法 #1:使用 collections.counter()

Python3
# Python code to demonstrate
# sort list by frequency
# of elements
 
from collections import Counter
 
ini_list = [1, 2, 3, 4, 4, 5, 5, 5, 5, 7,
            1, 1, 2, 4, 7, 8, 9, 6, 6, 6]
 
# printing initial ini_list
print ("initial list", str(ini_list))
 
# sorting on basis of frequency of elements
result = [item for items, c in Counter(ini_list).most_common()
                                      for item in [items] * c]
 
# printing final result
print("final list", str(result))


Python3
# Python code to demonstrate
# sort list by frequency
# of elements
 
from collections import Counter
from itertools import repeat, chain
 
ini_list = [1, 2, 3, 4, 4, 5, 5, 5, 5, 7,
            1, 1, 2, 4, 7, 8, 9, 6, 6, 6]
 
# printing initial ini_list
print ("initial list", str(ini_list))
 
# sorting on basis of frequency of elements
result = list(chain.from_iterable(repeat(i, c)
         for i, c in Counter(ini_list).most_common()))
 
# printing final result
print("final list", str(result))


Python3
# Python code to demonstrate
# sort list by frequency
# of elements
 
ini_list = [1, 1, 2, 2, 2, 3, 3, 3, 3, 3,
               5, 5, 5, 4, 4, 4, 4, 4, 4]
 
# printing initial ini_list
print ("initial list", str(ini_list))
 
# sorting on basis of frequency of elements
result = sorted(ini_list, key = ini_list.count,
                                reverse = True)
 
# printing final result
print("final list", str(result))


输出:

initial list [1, 2, 3, 4, 4, 5, 5, 5, 5, 7, 1, 1, 2, 4, 7, 8, 9, 6, 6, 6]
final list [5, 5, 5, 5, 1, 1, 1, 4, 4, 4, 6, 6, 6, 2, 2, 7, 7, 3, 8, 9]


方法 #2:使用可迭代对象

Python3

# Python code to demonstrate
# sort list by frequency
# of elements
 
from collections import Counter
from itertools import repeat, chain
 
ini_list = [1, 2, 3, 4, 4, 5, 5, 5, 5, 7,
            1, 1, 2, 4, 7, 8, 9, 6, 6, 6]
 
# printing initial ini_list
print ("initial list", str(ini_list))
 
# sorting on basis of frequency of elements
result = list(chain.from_iterable(repeat(i, c)
         for i, c in Counter(ini_list).most_common()))
 
# printing final result
print("final list", str(result))

输出:

initial list [1, 2, 3, 4, 4, 5, 5, 5, 5, 7, 1, 1, 2, 4, 7, 8, 9, 6, 6, 6]
final list [5, 5, 5, 5, 1, 1, 1, 4, 4, 4, 6, 6, 6, 2, 2, 7, 7, 3, 8, 9]


方法#3:使用排序

Python3

# Python code to demonstrate
# sort list by frequency
# of elements
 
ini_list = [1, 1, 2, 2, 2, 3, 3, 3, 3, 3,
               5, 5, 5, 4, 4, 4, 4, 4, 4]
 
# printing initial ini_list
print ("initial list", str(ini_list))
 
# sorting on basis of frequency of elements
result = sorted(ini_list, key = ini_list.count,
                                reverse = True)
 
# printing final result
print("final list", str(result))

输出:

initial list [1, 1, 2, 2, 2, 3, 3, 3, 3, 3, 5, 5, 5, 4, 4, 4, 4, 4, 4]
final list [4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 2, 2, 2, 5, 5, 5, 1, 1]