怕黑怪谈录,鸿蒙教尊,龟背竹
利用到了python中字典的collections.counter()函数
collections中函数counter的使用和用法:
counter工具用于支持便捷和快速地计数,
from collections import counter
cnt = counter()
for word in ['red', 'blue', 'red', 'green', 'blue', 'blue']:
cnt[word] += 1
print cnt
输出为
counter({'blue': 3, 'red': 2, 'green': 1})
快速实现了题目中所要求的只出现一次
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Python 实现将numpy中的nan和inf,nan替换成对应的均值
python爬虫把url链接编码成gbk2312格式过程解析
网友评论