python计数器Count

# -*- coding:utf-8 -*-
"""
python计数器Counter
需导入模块collections
"""
import collections # 统计各个字符出现的次数,以字典形式返回
obj = collections.Counter('adfsdfsdfswrwerwegfhgfhgh')
print obj
# elements => 原生的传入的值('adfsdfsdfswrwerwegfhgfhgh')
for v in obj.elements():
print v # 按参数给定的个数返回
print obj.most_common(4)
# 执行结果显示
Counter({'f': 5, 'd': 3, 'g': 3, 'h': 3, 's': 3, 'w': 3, 'e': 2, 'r': 2, 'a': 1})
[('f', 5), ('d', 3), ('g', 3), ('h', 3)]

python计数器Count的更多相关文章

  1. Python 元组 count() 方法

    描述 Python 元组 count() 方法用于统计某个元素在元祖中出现的次数. 语法 count() 方法语法: T.count(obj) 参数 obj -- 元祖中统计的对象. 返回值 返回元素 ...

  2. Python 列表 count() 方法

    描述 Python 列表 count() 方法用于统计某个元素在列表中出现的次数. 语法 count() 方法语法: L.count(obj) 参数 obj -- 列表中统计的对象. 返回值 返回元素 ...

  3. Python 字符串(count)

    字符串 count:(python中的count()函数,从字面上可以知道,他具有统计功能) Python count() 方法用于统计字符串里某个字符出现的次数.可选参数为在字符串搜索的开始与结束位 ...

  4. python sorted() count() set(list)-去重 -- search + match

    2.用python实现统计一篇英文文章内每个单词的出现频率,并返回出现频率最高的前10个单词及其出现次数,并解答以下问题?(标点符号可忽略) (1) 创建文件对象f后,解释f的readlines和xr ...

  5. Python List count()方法

    描述 count() 方法用于统计某个元素在列表中出现的次数.高佣联盟 www.cgewang.com 语法 count()方法语法: list.count(obj) 参数 obj -- 列表中统计的 ...

  6. python之count()函数

    # count()统计字符串中特定单词或短语出现次数(n = 3) strs = 'Good! Today is good day! Good job!' n = strs.lower().count ...

  7. python 计数器类Counter的用法

    简单操作: import collections A=['a','b','b','c','d','b','a'] count=collections.Counter(A) print(count) C ...

  8. python中count和index

    str = [1,2,3,4,5] #定义一个列表 str = 3 #列表3 str [1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5] str.count(1 ...

  9. python:count 函数

    API 一.string 中 某字符 的次数 str.count(sub, start= 0,end=len(string)) Args Annotations sub 搜索的子字符串 start 字 ...

随机推荐

  1. Codeforces 221 C. Little Elephant and Problem

    C. Little Elephant and Problem time limit per test 2 seconds memory limit per test 256 megabytes inp ...

  2. Spring boot初始

    1 创建pom.xml parent:org.springframework.boot  包含启动的依赖 添加依赖,如 spring-boot-starter-web mvn dependency:t ...

  3. 谨慎重载clone方法

    本文涉及到的概念 1.浅拷贝和深拷贝 2..clone方法的作用和使用方式 3.拷贝构造器和拷贝工厂   1.浅拷贝和深拷贝 浅拷贝 一个类实现Cloneable接口,然后,该类的实例调用clone方 ...

  4. 【BZOJ】3495: PA2010 Riddle 2-SAT算法

    [题意]有n个城镇被分成了k个郡,有m条连接城镇的无向边.要求给每个郡选择一个城镇作为首都,满足每条边至少有一个端点是首都.n,m,k<=10^6. [算法]2-SAT,前后缀优化建图 [题解] ...

  5. 【BZOJ】1426: 收集邮票 期望DP

    [题意]有n种不同的邮票,第i次可以花i元等概率购买到一种邮票,求集齐n种邮票的期望代价.n<=10^4. [算法]期望DP [题解]首先设g[i]表示已拥有i张邮票集齐的期望购买次数,根据全期 ...

  6. Redis数据类型之字符串(string)

    1. string类型简介 string类型是二进制安全的,能够存储任意类型的字符串. string类型是最常用到的数据类型,一种常用的用法就是将对象格式化为JSON字符串然后放到redis中,取出来 ...

  7. MySQL之——如何添加新数据库到MySQL主从复制列表 【转】

    转自 转载请注明出处:http://blog.csdn.net/l1028386804/article/details/54653691 MySQL主从复制一般情况下我们会设置需要同步的数据库,使用参 ...

  8. 123.Best Time to Buy and Sell Stock III---dp

    题目链接:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/description/ 题目大意:与122题类似,只是这 ...

  9. js + -操作符

    js + 举例说明最有效了... "11"+1='111' "11"+'1'="111" 11+1=12 大概的感觉就是+操作符会优先输入S ...

  10. Porting of cURL to Android OS using NDK (from The Software Rogue)

    Porting of cURL to Android OS using NDK   In continuing my journey into Android territory, I decided ...