[Python] Finding the most common elements in an iterable
2024-09-07 03:05:15
>>> import collections >>> # Tally occurrences of words in a list
>>> cnt = collections.Counter()
>>> for word in ['red', 'blue', 'red', 'green', 'blue', 'blue']:
... cnt[word] += 1
>>> cnt
Counter({'blue': 3, 'red': 2, 'green': 1}) >>> c = collections.Counter('helloworld') >>> c
Counter({'l': 3, 'o': 2, 'e': 1, 'd': 1, 'h': 1, 'r': 1, 'w': 1}) >>> c.most_common(3)
[('l', 3), ('o', 2), ('e', 1)]
https://docs.python.org/2/library/collections.html
最新文章
- 发布Live Writer代码着色插件CNBlogs.CodeHighlighter
- ASP.NET MVC视图中的@Html.xxx(...)
- placement new讲解
- ubuntu sublime-text package control
- Android 调用系统的邮箱app发送邮件
- jquery and event
- Linux 命令 快捷命令综合
- POJ1163 The Triangle 【DP】
- 解释器模式(Interpreter)
- javascript 函数详解
- postgresql 53300错误
- React-Native的基本控件属性方法,对React-Native的学习,从熟悉基本控件开始。
- 阿里巴巴的26款超神Java开源项目!
- vue+vue-cli+淘宝lib-flexible做移动端自适应
- IdentityServer4-从数据库获取User进行授权验证(五)
- LeetCode-860. Lemonade Change
- 第 3 章 镜像 - 017 - RUN vs CMD vs ENTRYPOINT
- JS滚轮事件onmousewheel
- python learning Process and Thread.py
- Django中模型(一)