[Python] Finding the most common elements in an iterable
>>> 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的更多相关文章
- [LeetCode]题解(python):014-Longest Common Prefix
题目来源: https://leetcode.com/problems/longest-common-prefix/ 题意分析: 这道题目是要写一个函数,找出字符串组strs的最长公共前缀子字符串. ...
- [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 ...
- 【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 ...
- Python标准库:内置函数dict(iterable, **kwarg)
本函数是从可迭代对象来创建新字典.比方一个元组组成的列表,或者一个字典对象. 样例: #dict() #以键对方式构造字典 d1 = dict(one = 1, two = 2, a = 3) pri ...
- Python标准库:内置函数set([iterable])
本函数是从迭代对象生成集合.集合能够添加或删除元素. 样例: #set() tset = set([1, 2, 3, 3, 4, 5, 6, 6]) print(tset) tset.add(20) ...
- Python标准库:内置函数tuple([iterable])
本函数实现从可迭代对象生成一个元组对象返回.元组对象是一个不可改动的列表对象. 样例: #tuple() print(tuple([1, 2, 3])) print(tuple((1, 2, 3))) ...
- 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 ...
- Python标准库:内置函数all(iterable)
假设可迭代的对象的所有元素所有非空(或者空迭代对象),就返回True.这个函数主要用来推断列表.元组.字典等对象是否有空元素.比方有10000个元素的列表,假设没有提供此函数,须要使用循环来实现.那么 ...
- [ZZ] python 语言技巧
http://sahandsaba.com/thirty-python-language-features-and-tricks-you-may-not-know.html 感谢原作者 30 Pyt ...
随机推荐
- Linux/UNIX线程(1)
线程(1) 本文将介绍怎样使用多个控制线程在单个进程环境中运行多个任务. 一个进程中的全部线程都能够訪问该进程的组成部件(如文件描写叙述符和内存). 线程包含了表示进程内运行环境必须的信息,当中包含进 ...
- reverse(两种反向生成url django原生形式和rest_framework中版本的形式)
reverse(两种反向生成url django原生形式和rest_framework中版本的形式) views.py from django.shortcuts import render,Http ...
- POJ 3661 DP
题意: 思路: i表示到了i,j表示疲劳度为j f[i][j]表示能跑的最大距离 f[i][j]=f[i-1][j-1]+a[i] if(i-j>=0)f[i][0]=max(f[i][0],f ...
- js 快捷键设置
function hotkey() { var a=window.event.keyCode; if((a==65)&&(event.ctrlKey)) { alert("你 ...
- 【APP自动化】Appium Android 元素定位方法 原生+H5
参考资料: http://blog.csdn.net/vivian_ljx/article/details/54410024
- conda常用命令,如何在conda环境中安装gym库?
查看已安装的环境: conda info -e 或 conda env list 创建新环境gymlab: conda create -n gymlab python=3.5 激活环境gymlab: ...
- python 任何基础问题,包括语法等
*)copy()和deep copy() 参考链接:https://blog.csdn.net/qq_32907349/article/details/52190796 *)OPP面向对象编程 *)接 ...
- 【ICM Technex 2018 and Codeforces Round #463 (Div. 1 + Div. 2, combined) A】 Palindromic Supersequence
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 字符串倒着加到原串右边就好 [代码] #include <bits/stdc++.h> using namespace ...
- 洛谷 P1824 进击的奶牛
P1824 进击的奶牛 题目描述 Farmer John建造了一个有N(2<=N<=100,000)个隔间的牛棚,这些隔间分布在一条直线上,坐标是x1,...,xN (0<=xi&l ...
- [Python] Slice the data with pandas
For example we have dataframe like this: SPY AAPL IBM GOOG GLD 2017-01-03 222.073914 114.311760 160. ...