>>> 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

[Python] Finding the most common elements in an iterable的更多相关文章

  1. [LeetCode]题解(python):014-Longest Common Prefix

    题目来源: https://leetcode.com/problems/longest-common-prefix/ 题意分析: 这道题目是要写一个函数,找出字符串组strs的最长公共前缀子字符串. ...

  2. [LeetCode&Python] Problem 235. Lowest Common Ancestor of a Binary Search Tree

    Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...

  3. 【leetcode❤python】235. Lowest Common Ancestor of a Binary Search Tree

    #-*- coding: UTF-8 -*- # Definition for a binary tree node.# class TreeNode(object):#     def __init ...

  4. Python标准库:内置函数dict(iterable, **kwarg)

    本函数是从可迭代对象来创建新字典.比方一个元组组成的列表,或者一个字典对象. 样例: #dict() #以键对方式构造字典 d1 = dict(one = 1, two = 2, a = 3) pri ...

  5. Python标准库:内置函数set([iterable])

    本函数是从迭代对象生成集合.集合能够添加或删除元素. 样例: #set() tset = set([1, 2, 3, 3, 4, 5, 6, 6]) print(tset) tset.add(20) ...

  6. Python标准库:内置函数tuple([iterable])

    本函数实现从可迭代对象生成一个元组对象返回.元组对象是一个不可改动的列表对象. 样例: #tuple() print(tuple([1, 2, 3])) print(tuple((1, 2, 3))) ...

  7. python TypeError: 'builtin_function_or_method' object is not iterable keys

    statinfo = os.stat( OneFilePath ) if AllFiles.has_key( statinfo.st_size ): OneKey = AllFiles[ statin ...

  8. Python标准库:内置函数all(iterable)

    假设可迭代的对象的所有元素所有非空(或者空迭代对象),就返回True.这个函数主要用来推断列表.元组.字典等对象是否有空元素.比方有10000个元素的列表,假设没有提供此函数,须要使用循环来实现.那么 ...

  9. [ZZ] python 语言技巧

    http://sahandsaba.com/thirty-python-language-features-and-tricks-you-may-not-know.html  感谢原作者 30 Pyt ...

随机推荐

  1. [luogu P2586] GCD 解题报告 (莫比乌斯反演|欧拉函数)

    题目链接:https://www.luogu.org/problemnew/show/P2568#sub 题目大意: 计算​$\sum_{x=1}^n\sum_{y=1}^n [gcd(x,y)==p ...

  2. 4.STL六大组件

    代码示例 #include <vector> #include <list> #include <iostream> #include <algorithm& ...

  3. Python Schema使用说明

    转自https://segmentfault.com/a/1190000011777230 Python Schema使用说明 Schema是什么? 不管我们做什么应用,只要和用户输入打交道,就有一个 ...

  4. 基于JS的身份证验证(完整版)

    var Wi = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1 ]; // 加权因子 var ValideCode = [ 1, 0 ...

  5. go语言中在变量后加上接口是什么意思?

    如题刚刚开始学习go 语言有些不懂: a.Data = make(map[string]interface{}) 我认为它是在申请a.Data map为字符串类型的空间,那么它后面接一个空的inter ...

  6. 《剑指offer》调整数组顺序使奇数位于偶数前面

    一.题目描述 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变. 二.输入描述 ...

  7. bzoj1051 [HAOI2006]受欢迎的牛 tarjan&&缩点

    题目描述 每头奶牛都梦想成为牛棚里的明星.被所有奶牛喜欢的奶牛就是一头明星奶牛.所有奶 牛都是自恋狂,每头奶牛总是喜欢自己的.奶牛之间的“喜欢”是可以传递的——如果A喜 欢B,B喜欢C,那么A也喜欢C ...

  8. zabbix 使用自带模板监控mysql

    1.这里可以采用zabbix自带的mysql模版,但是也需要在mysql服务器上准备获取mysql status的脚本chk_mysql.sh,zabbix通过调用这个脚本来获取mysql的运行信息. ...

  9. Win 7系统倒计时!

    3月25日消息,近日微软已经开始通知当前正在使用Windows 7的用户,该操作系统“接近尾声”.微软表示计划在2020年1月14日终止对Windows 7的所有支持.但结束Windows 7似乎并不 ...

  10. 15 hbase 学习(十五)缓存机制以及可以利用SSD作为存储的BucketCache

    下面介绍Hbase的缓存机制:  a.HBase在读取时,会以Block为单位进行cache,用来提升读的性能 b.Block可以分类为DataBlock(默认大小64K,存储KV).BloomBlo ...