from collections import Counter
arr = [1,2,5,1,1,5,6,3,3,2,2,4,8]
# 方法一:利用python自带的包来解决
res1 = Counter(arr)
# 方法二:自定义函数
def count_char(arr):
res = dict()
for i in arr:
if i not in res.keys():
res[i] = 0
res[i] += 1
return res
res2 = count_char(arr)
python统计某一元素出现的次数
最新推荐文章于 2024-08-04 14:39:00 发布