>>> 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. hdu_1394,线段树求逆序数

    http://www.notonlysuccess.com/index.php/segment-tree-complete/ #include<iostream> #include< ...

  2. OneNote

    OneNote导致生成OneNote Table Of Contents.onetoc2 https://answers.microsoft.com/en-us/msoffice/forum/all/ ...

  3. hdoj--2098--分拆素数和(水题)

    分拆素数和 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

  4. django 笔记15 ajax系列

    参考 http://www.cnblogs.com/wupeiqi/articles/5703697.html # 原生操作# jQuery操作# 伪Ajax操作# XMLHttpReques 伪aj ...

  5. POJ 3273 二分答案

    思路:二分答案经典题吧....注意边界就OK了 //By SiriusRen #include <cstdio> #include <algorithm> using name ...

  6. mysql读写分离的解决方案

    来源于网上整理 http://yanwt.iteye.com/blog/1460780 现有三种解决方式实现mysql读写分离 1 程序修改mysql操作类 优点:直接和数据库通信,简单快捷的读写分离 ...

  7. UVa 140 Bandwidth【枚举排列】

    题意:给出n个节点的图,和一个节点的排列,定义节点i的带宽b[i]为i和其相邻节点在排列中的最远的距离,所有的b[i]的最大值为这个图的带宽,给一个图,求出带宽最小的节点排列 看的紫书,紫书上说得很详 ...

  8. linux系统下,11款常见远程桌面控制软件(转载)

    远程控制能够给人们带来很多便利,本文介绍了11款常见的Linux系统下的远程桌面控制工具,总有一款能适合您. 一. Grdc 它是一个用GTK+编写的,适用于gnome桌面环境的远程桌面访问软件.看图 ...

  9. laravel 自定义全局函数

    在 app 目录下创建一个 Helpers 目录,在此目录下创建文件.这些文件就是全局函数文件.如叫:function.php 加载此文件: 1 . 在 bootstrap/autoload.php ...

  10. MySQL 大数据量文本插入

    导入几万条数据需要等好几分钟的朋友来围观一下! 百万条数据插入,只在一瞬间.呵呵夸张,夸张!! 不到半分钟是真的! 插入指令: load data infile 'c:/wamp/tmp/Data_O ...