Python|列出元素的频率
有时我们有一个实用程序,我们需要在其中找到列表中元素的频率,并且这个问题的解决方案已经讨论了很多次。但有时我们会遇到需要查找特定元素出现的列表数量的任务。让我们讨论一些可以做到这一点的速记。
方法 #1:使用 Counter() + set() + 列表推导
上述功能的组合可用于执行任务。 Counter函数进行分组,set函数将不同的元素提取为 dict 的键,并检查其列表出现的列表理解。
Python3
# Python3 code to demonstrate
# list frequency of elements
# using Counter() + set() + list comprehension
from collections import Counter
# initializing list
test_list = [[3, 5, 4],
[6, 2, 4],
[1, 3, 6]]
# printing original list
print("The original list : " + str(test_list))
# using Counter() + set() + list comprehension
# list frequency of elements
res = dict(Counter(i for sub in test_list for i in set(sub)))
# printing result
print("The list frequency of elements is : " + str(res))
Python3
# Python3 code to demonstrate
# list frequency of elements
# using Counter() + itertools.chain.from_iterable() + map() + set()
from collections import Counter
from itertools import chain
# initializing list
test_list = [[3, 5, 4],
[6, 2, 4],
[1, 3, 6]]
# printing original list
print("The original list : " + str(test_list))
# using Counter() + itertools.chain.from_iterable() + map() + set()
# list frequency of elements
res = dict(Counter(chain.from_iterable(map(set, test_list))))
# printing result
print("The list frequency of elements is : " + str(res))
Python3
d = {}
test_list = [[3, 5, 4],
[6, 2, 4],
[1, 3, 6]]
for x in test_list:
for i in x:
d[i] = d.get(i,0) + 1
# Original list
print(f"The original list : {test_list}" )
# printing result
print(f"The list frequency of elements is : {d}" )
Python3
import pandas as pd
test_list = [3,5,4,3,3,4,5,2]
df1 = pd.Series(test_list).value_counts().sort_index().reset_index().reset_index(drop=True)
df1.columns = ['Element', 'Frequency']
# Original list
print(f"The original list : {test_list}" )
# printing result
print(f"The list frequency of elements is :\n {df1.to_string(index=False)}" )
输出 :
The original list : [[3, 5, 4], [6, 2, 4], [1, 3, 6]]
The list frequency of elements is : {1: 1, 2: 1, 3: 2, 4: 2, 5: 1, 6: 2}
方法#2:使用 Counter() + itertools.chain.from_iterable() + map() + set()
上述 4 种功能也可以结合起来实现这一特定任务。 set函数提取由 Counter 形成的字典键,map函数执行所有子列表的任务,而 from_iterable函数使用迭代器执行,这比列表理解更快。
Python3
# Python3 code to demonstrate
# list frequency of elements
# using Counter() + itertools.chain.from_iterable() + map() + set()
from collections import Counter
from itertools import chain
# initializing list
test_list = [[3, 5, 4],
[6, 2, 4],
[1, 3, 6]]
# printing original list
print("The original list : " + str(test_list))
# using Counter() + itertools.chain.from_iterable() + map() + set()
# list frequency of elements
res = dict(Counter(chain.from_iterable(map(set, test_list))))
# printing result
print("The list frequency of elements is : " + str(res))
输出 :
The original list : [[3, 5, 4], [6, 2, 4], [1, 3, 6]]
The list frequency of elements is : {1: 1, 2: 1, 3: 2, 4: 2, 5: 1, 6: 2}
方法#3:使用Python字典+get()方法
Python字典提供了一个 get 方法,该方法返回与键对应的值,如果字典中不存在该键,则它提供创建键并为其分配默认值的功能。我们将使用字典的这个功能。
Python3
d = {}
test_list = [[3, 5, 4],
[6, 2, 4],
[1, 3, 6]]
for x in test_list:
for i in x:
d[i] = d.get(i,0) + 1
# Original list
print(f"The original list : {test_list}" )
# printing result
print(f"The list frequency of elements is : {d}" )
输出:
The original list : [[3, 5, 4], [6, 2, 4], [1, 3, 6]]
The list frequency of elements is : {3: 2, 5: 1, 4: 2, 6: 2, 2: 1, 1: 1}
方法 #4:使用 Pandas
在这个方法中,我们将使用一个名为 pandas 的Python模块(您可以在本文中了解更多关于 pandas 的信息)来查找给定数据的频率,下面是它的代码。
Python3
import pandas as pd
test_list = [3,5,4,3,3,4,5,2]
df1 = pd.Series(test_list).value_counts().sort_index().reset_index().reset_index(drop=True)
df1.columns = ['Element', 'Frequency']
# Original list
print(f"The original list : {test_list}" )
# printing result
print(f"The list frequency of elements is :\n {df1.to_string(index=False)}" )
输出:
The original list : [3, 5, 4, 3, 3, 4, 5, 2]
The list frequency of elements is :
Element Frequency
2 1
3 3
4 2
5 2