python统计list中个元素出现次数
2019-04-12 10:47
1156 查看
利用字典统计
利用pandas统计
利用Counter类统计
1:方法一
data = [1,2,3,4,2,3,4,2,3,4,5,6,6,4,4,5,6,7,4] data_dict = {} for key in data: data_dict[key] = data_dict.get(key,0) + 1 print("data_dict:",data_dict)
输出结果:
data_dict: {1: 1, 2: 3, 3: 3, 4: 6, 5: 2, 6: 3, 7: 1}
2:方法二
import pandas as pd data = [1,2,3,4,2,3,4,2,3,4,5,6,6,4,4,5,6,7,4] result = pd.value_counts(data) print("result:",result)
输出结果:
result: 4 6
6 3
3 3
2 3
5 2
7 1
1 1
dtype: int64
3:方法三:
from collections import Counter data = [1,2,3,4,2,3,4,2,3,4,5,6,6,4,4,5,6,7,4] result = Counter(data) print("result:",result)
输出结果:
result: Counter({4: 6, 2: 3, 3: 3, 6: 3, 5: 2, 1: 1, 7: 1})
相关文章推荐
- python 统计列表(list)中元素出现的次数,并排序
- python 统计list中各个元素出现的次数
- List之统计元素项以及元素出现的次数
- java list 统计元素出现次数
- 统计List中的元素重复出现的次数
- python统计数组元素出现次数以及排序
- Python统计列表元素出现次数
- Python统计列表元素出现次数
- java统计List中的元素重复出现的次数
- 【Python】利用count函数求list中每个元素出现的次数,求众数的改进
- Python中求一个列表中的众数,并统计元素出现的次数存入字典,最后将字典的值降序排序
- java统计List中的元素重复出现的次数和对map按key或键值排序
- Python3 Counter类计算list中元素出现次数
- python 统计数组中元素出现次数并进行排序的实例
- python 统计数组中元素出现次数并进行排序
- python 统计列表中各元素出现的次数
- Python统计列表(List)中的重复项出现的次数并进行排序
- python【3】-统计序列中元素出现的次数(频度)
- python字典分别按照key和value升序或者降序排列--以给定一个含有重复元素的数组list,按照其出现次数大小降序输出
- Python统计列表中元素出现的次数