#count对象 Only 2.7
from collections import Counter
#统计字母出现的次数
Counter('hello world')
Counter(['red', 'blue', 'red', 'green', 'blue', 'blue']) #小于等于0的会被忽略
c = Counter(a=4, b=2, c=0, d=-2)
list(c.elements())
#取前三个最多的字母
Counter('hello world').most_common(3) #堆
from collections import deque
d = deque('abc')
d.append('d')
d.pop() #后入先出
d.popleft() #先入先出 #返回最后n行文本
deque(open(filename), n) #defaultdict
from collections import defaultdict
#使用list初始化一个dict
d = defaultdict(list)
d["yellow"].append(1)
d["red"].append(2)
d["yellow"].append(3) print d.items() #[('red', [2]), ('yellow', [1, 3])]
#用int初始华一个dict
d = defaultdict(int)
d["yellow"] += 1
d["red"] += 2
d["yellow"] += 3
print d.items() #[('red', 2), ('yellow', 4)]

Python collections的更多相关文章

  1. python collections defaultdict

    class_counts  = defaultdict(int) 一.关于defaultdict 在Python里面有一个模块collections,解释是数据类型容器模块.这里面有一个collect ...

  2. Python collections模块总结

    Python collections模块总结 除了我们使用的那些基础的数据结构,还有包括其它的一些模块提供的数据结构,有时甚至比基础的数据结构还要好用. collections ChainMap 这是 ...

  3. (转)python collections模块详解

    python collections模块详解 原文:http://www.cnblogs.com/dahu-daqing/p/7040490.html 1.模块简介 collections包含了一些特 ...

  4. Python Collections详解

    Python Collections详解 collections模块在内置数据结构(list.tuple.dict.set)的基础上,提供了几个额外的数据结构:ChainMap.Counter.deq ...

  5. python collections模块

    collections模块基本介绍 collections在通用的容器dict,list,set和tuple之上提供了几个可选的数据类型 namedtuple() factory function f ...

  6. Python collections 模块用法举例

    Python作为一个“内置电池”的编程语言,标准库里面拥有非常多好用的模块.比如今天想给大家 介绍的 collections 就是一个非常好的例子. 1.collections模块基本介绍 我们都知道 ...

  7. Python collections.defaultdict 笔记

    其实defaultdict 就是一个字典,只不过python自动的为它的键赋了一个初始值.这也就是说,你不显示的为字典的键赋初值python不会报错,看下实际例子. 比如你想计算频率 frequenc ...

  8. Python collections.defaultdict() 与 dict的使用和区别

    看样子这个文档是难以看懂了.直接看示例: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 import collections s = [('yellow', ...

  9. Python collections.OrderedDict解决dict元素顺序问题

    编程中遇到个问题,python json.loads时元素顺序可能会发生变化. 这个对于一些需要使用元素顺序来做一些策略的代码来说是致命的. 在网上查了查,结合自己的知识总结一下. 使用dict时,K ...

  10. Python: collections.nametuple()--映射名称到序列元素

    问题:  通过下标访问列表或者元组中元素 answer: collections.namedtuple()通过使用元组对象来解决这个问题 这个函数实际上是一个返回Python中标准元组类型子类的一个工 ...

随机推荐

  1. Json数据导出生成Excel

    最近在做一个导入导出Excel的功能,导出其他类型的文件都比较熟悉,但是导入跟导出一个Excel还是稍微特殊点.根据这次的经验,写了个导出的小样例. 总体思路就是json数据的key,value跟Ex ...

  2. C#之WinForm界面分辨率问题

    在做上一个C#小工具的时候,当时为了处理界面最大化,分辨率问题,只是简单的用各种···Panle控价简单随意的处理控件的大小位置,字体什么的就随缘了(貌似有点不负责任啊,嘿嘿~). 所以在开始第二个C ...

  3. How can I list colors in WPF with XAML?

    How can I get list of all colors I can pick in Visual Studio Designer (which is System.Windows.Media ...

  4. [转]Passing Managed Structures With Strings To Unmanaged Code Part 2

    1. Introduction. 1.1 In part 1 of this series of blogs we studied how to pass a managed structure (w ...

  5. 自定义非等高 Cell

    1.自定义非等高 Cell介绍 1.1 代码自定义(frame) 新建一个继承自 UITableViewCell 的类. 重写 initWithStyle:reuseIdentifier: 方法. 添 ...

  6. [SinGuLaRiTy] 2017-07-22 NOIP2017 模拟赛

    [SInGuLaRiTy-1029] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. <全是看看代码就会的水题,偷个懒不单独写题解了~& ...

  7. loj #2021. 「AHOI / HNOI2017」大佬

    #2021. 「AHOI / HNOI2017」大佬   题目描述 人们总是难免会碰到大佬.他们趾高气昂地谈论凡人不能理解的算法和数据结构,走到任何一个地方,大佬的气场就能让周围的人吓得瑟瑟发抖,不敢 ...

  8. Shell---自动测试局域网内的网络连通情况

    #!/bin/bash );do RE=`ping .$i` echo $RE >>result.log done

  9. CentOS下安装配置SVN服务器并自动同步到web目录

    一.安装 yum install subversion测试是否安装成功 /usr/bin/svnserve --version如提示以下内容,说明已安装成功 svnserve,版本 1.6.11 (r ...

  10. php版 日文半角转全角

    工作需要,写的这个 /* *转载请注明 http://www.cnblogs.com/kclteam/p/5278923.html$str //参数可以是字符串或数组*/ function HkToF ...