首页 > 编程语言 > 详细

Python统计列表中元素出现的次数

时间:2015-01-22 11:14:49      阅读:293      评论:0      收藏:0      [点我收藏+]

Python列表可以进行简单的统计,比如list的函数count()可以直接统计元素出现的次数。


mylist = [2,2,2,2,2,2,3,3,3,3]

myset = set(mylist) #删除列表中的重复元素   print myset    set([2, 3])

for item in myset: 

     print mylist.count(item), " of ", item, " in list"


打印结果:

6 of 2 in list  # 2出现在mylist中6次 

4 of 3 in list # 3出现在mylist中4次


Python统计列表中元素出现的次数

原文:http://blog.csdn.net/maoyongfan3/article/details/43015201

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!