Python--day19--collections模块
常用模块一的各个模块解释:

文件名不要起跟模块名一样:(模块本身就是一个py文件)

collection模块:
namedtuple方法:

例1:

例2:

dequeue方法:双端队列


有序字典OrderedDict:


defaultdict默认字典:


values = [11, 22, 33,44,55,66,77,88,99,90]
my_dict = {}
for value in values:
if value>66:
if my_dict.has_key('k1'):
my_dict['k1'].append(value)
else:
my_dict['k1'] = [value]
else:
if my_dict.has_key('k2'):
my_dict['k2'].append(value)
else:
my_dict['k2'] = [value]
原生代码
from collections import defaultdict values = [11, 22, 33,44,55,66,77,88,99,90] my_dict = defaultdict(list) for value in values:
if value>66:
my_dict['k1'].append(value)
else:
my_dict['k2'].append(value)
defaultdict字典解决方法
使用dict时,如果引用的Key不存在,就会抛出KeyError。如果希望key不存在时,返回一个默认值,就可以用defaultdict:
>>> from collections import defaultdict
>>> dd = defaultdict(lambda: 'N/A')
>>> dd['key1'] = 'abc'
>>> dd['key1'] # key1存在
'abc'
>>> dd['key2'] # key2不存在,返回默认值
'N/A'
例2
Counter方法:计数

队列:queue 先进先出(没有索引)

Python--day19--collections模块的更多相关文章
- Python中collections模块
目录 Python中collections模块 Counter defaultdict OrderedDict namedtuple deque ChainMap Python中collections ...
- Python的collections模块中namedtuple结构使用示例
namedtuple顾名思义,就是名字+元组的数据结构,下面就来看一下Python的collections模块中namedtuple结构使用示例 namedtuple 就是命名的 tuple,比较 ...
- python:collections模块
Counter类 介绍:A counter tool is provided to support convenient and rapid tallies 构造:class collections. ...
- python之collections模块(OrderDict,defaultdict)
前言: import collections print([name for name in dir(collections) if not name.startswith("_" ...
- 转载:Python中collections模块
转载自:Python中collections模块 目录 Python中collections模块 Counter defaultdict OrderedDict namedtuple deque Ch ...
- python的Collections 模块
Collections 模块 知识点 Counter 类 defaultdict 类 namedtuple 类 在这个实验我们会学习 Collections 模块.这个模块实现了一些很好的数据结构,它 ...
- Python中collections模块的使用
本文将详细讲解collections模块中的所有类,和每个类中的方法,从源码和性能的角度剖析. 一个模块主要用来干嘛,有哪些类可以使用,看__init__.py就知道 '''This module i ...
- python 之 Collections模块
官方文档:https://yiyibooks.cn/xx/python_352/library/collections.html 参考: https://blog.csdn.net/songfreem ...
- 【python】collections模块(有序字典,计数器,双向队列)
collections模块基本介绍 我们都知道,Python拥有一些内置的数据类型,比如str, int, list, tuple, dict等, collections模块在这些内置数据类型的基础上 ...
- Python中Collections模块的Counter容器类使用教程
1.collections模块 collections模块自Python 2.4版本开始被引入,包含了dict.set.list.tuple以外的一些特殊的容器类型,分别是: OrderedDict类 ...
随机推荐
- ML面试1000题系列(61-70)
本文总结ML面试常见的问题集 转载来源:https://blog.csdn.net/v_july_v/article/details/78121924 61.说说共轭梯度法? @wtq1993,htt ...
- [leetcode] Reverse Words in a String [1]
一.题目: Given an input string, reverse the string word by word. For example, Given s = "the sky i ...
- 一款优秀的前端JS框架—AngularJS
前 言 AngularJS 是一个 JavaScript 框架.它可通过 <script> 标签添加到 HTML 页面. AngularJS 通过 指令 扩展了 HTML,且通过 表 ...
- python 常规字符匹配
- NOIP模拟 17.8.17
NOIP模拟17.8.17 A 小 G 的字符串文件名 输入文件 输出文件 时间限制 空间限制str.pas/c/cpp str.in str.out 1s 128MB[题目描述]有一天,小 L 给小 ...
- node.js对象数据类型
在这里复习下前端JS的数据类型:前端JS中的数据类型: 1.基本/原生/值类型 string.number.boolean.null.undefined 2.引用/对象类型 ES对象类型:Str ...
- fedora 安装 pidgin-lwqq
因为 腾讯自带的linux for qq 已经无法登陆,于是详尽各种办法在fedora 上安装 qq ,但均以失败高中.于是使用了几天web qq 但最终无法忍受,于是有研究起来了 pidgin-l ...
- More Effective C++: 02操作符
05:谨慎定义类型转换函数 有两种函数允许编译器进行隐式类型转换:单参数构造函数(single-argument constructors)和隐式类型转换运算符.单参数构造函数是指只用一个参数即可以调 ...
- KiCad EDA 过孔是否可以开窗?
KiCad EDA 过孔是否可以开窗? 和传统的商业 EDA 不同,KiCad EDA 的过孔默认就是盖绿油. 在 KiCad 的过孔界面没有任何可以设置的地方,这也有一个好处,不过考虑过孔是否盖油. ...
- Java数据类型分析
Java的简单数据讲解列表如下: int:int为整数类型,存储的时候,用4个字节存储,范围为-2,147,483,648到2,147,483,647,在变量初始化的时候,int类型的默认值为0. ...