📜  Python|根据频率对列表元素进行分组

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

Python|根据频率对列表元素进行分组

给定一个元素列表,编写一个Python程序将列表元素及其在元组中的相应频率分组。

例子:

Input : [1, 3, 4, 4, 1, 5, 3, 1]
Output : [(1, 3), (3, 2), (4, 2), (5, 1)]

Input : ['x', 'a', 'x', 'y', 'a', 'x']
Output : [('x', 3), ('a', 2), ('y', 1)]

方法#1:列表理解

我们可以使用列表推导来形成每个元素的元组及其出现次数并将其存储在“res”中,但这将包含重复的第一个元素。因此,要删除重复的第一个元素,我们使用OrderedDict(res).items()

# Python3 program to Grouping list 
# elements based on frequency
from collections import OrderedDict 
  
def group_list(lst):
      
    res =  [(el, lst.count(el)) for el in lst]
    return list(OrderedDict(res).items())
      
# Driver code
lst = [1, 3, 4, 4, 1, 5, 3, 1]
print(group_list(lst))
输出:
[(1, 3), (3, 2), (4, 2), (5, 1)]


方法 #2:使用collections.Counter()

collections.Counter()提供了两个直接方法keys()values()提供元素及其出现。最后,使用Python zip()方法将它们压缩在一起。

# Python3 program to Grouping list 
# elements based on frequency
from collections import Counter
  
def group_list(lst):
      
    return list(zip(Counter(lst).keys(), Counter(lst).values()))
      
# Driver code
lst = [1, 3, 4, 4, 1, 5, 3, 1]
print(group_list(lst))
输出:
[(1, 3), (3, 2), (4, 2), (5, 1)]