One - One Code All

Blog Content

python 判断数字出现的次数

Python   2012-10-02 19:18:13

判断是否为数字:str.isdigit()


方法1:

list1 = [1,1,2,2,2,2,3,3,3]
list2 = set(list1)
for i in list1:
    list1.count(i)

方法2:

from collections import Counter
Counter(list1)

方法3:

d = {}
for i in list1:
  if list1.count(i)>1:
    d[i] = list1.count(i)

基本都有用到字典来实现。


上一篇:python获取前一天或前N天的日期
下一篇:pythonic 是什么?

The minute you think of giving up, think of the reason why you held on so long.