2021
02-21
02-21
python 统计list中各个元素出现的次数的几种方法
利用字典dict来完成统计举例:a=[1,2,3,1,1,2]dict={}forkeyina:dict[key]=dict.get(key,0)+1printdict输出结果:>>>{1:3,2:2,3:1}利用Python的collection包下Counter的类举例:fromcollectionsimportCountera=[1,2,3,1,1,2]result=Counter(a)printresult输出结果:>>>{1:3,2:2,3:1}Python的pandas包下的value_counts方法举例:importpandasaspda=[1,2,3,1,1,2]result=pd....
继续阅读 >