笔记-python-standard library-8.3.collections

1.      collections简介

Source code: Lib/collections/__init__.py


This module implements specialized container datatypes providing alternatives to Python’s general purpose built-in containers, dict, list, set, and tuple.

实现了一些特殊的容器类。

namedtuple()

factory function for creating tuple subclasses with named fields

deque

list-like container with fast appends and pops on either end

ChainMap

dict-like class for creating a single view of multiple mappings

Counter

dict subclass for counting hashable objects

OrderedDict

dict subclass that remembers the order entries were added

defaultdict

dict subclass that calls a factory function to supply missing values

UserDict

wrapper around dictionary objects for easier dict subclassing

UserList

wrapper around list objects for easier list subclassing

UserString

wrapper around string objects for easier string subclassing

1.1.    namedtuple

Named tuples assign meaning to each position in a tuple and allow for more readable, self-documenting code. They can be used wherever regular tuples are used, and they add the ability to access fields by name instead of position index.

collections.namedtuple(typename, field_names, *, rename=False, defaults=None, module=None)

它主要用于增加可读性,可以通过属性来引用元素。

ct = collections.namedtuple('point', ['x', 'y', 'z'])
p1 = ct(1,2,3)
tu = ('www','www',6)
p2 = ct._make(tu)
p3 = ct(*tu)
pr_type(p1)
pr_type(p2)
pr_type(p3)

pr_type(p1.x)

创建的ct对象是tuple的一种子类:

print(isinstance(p1, ct))
print(issubclass(ct, tuple))

True

True

1.2.   
deque

class collections.deque([iterable[, maxlen]])

Deques are a generalization of stacks and
queues (the name is pronounced “deck” and is short for “double-ended queue”).

Deques support thread-safe, memory
efficient appends and pops from either side of the deque with approximately the
same O(1) performance in either direction.

它支持线程安全,内存高效的添加删除,无论从哪一端操作,都近似于O(1)操作。

Though list objects support
similar operations, they are optimized for fast fixed-length operations and
incur O(n) memory movement costs
for pop(0) and insert(0, v) operations which change
both the size and position of the underlying data representation.

deque是为了高效实现插入和删除操作的双向列表,适合用于队列和栈:

If maxlen is not specified or is None,
deques may grow to an arbitrary length. Otherwise, the deque is bounded to the
specified maximum length. Once a bounded length deque is full, when new items
are added, a corresponding number of items are discarded from the opposite end.
Bounded length deques provide functionality similar to the tail filter in Unix.
They are also useful for tracking transactions and other pools of data where only
the most recent activity is of interest.

如果不指定maxlen参数,dqueues不限长度;反之则有长度限制;

如果存在队列已满,插入新元素意味着从另一端丢弃一个元素。

Deque objects support the following
methods:

append(x)

Add x to the right side of the deque.

appendleft(x)

Add x to the left side of the deque.

clear()

Remove all elements from the deque leaving
it with length 0.

copy()

Create a shallow copy of the deque.

count(x)

Count the number of deque elements equal to
x.

New in version 3.2.

extend(iterable)

Extend the right side of the deque by
appending elements from the iterable argument.

extendleft(iterable)

Extend the left side of the deque by
appending elements from iterable. Note, the series of left appends results in
reversing the order of elements in the iterable argument.

index(x[, start[, stop]])

Return the position of x in the deque (at
or after index start and before index stop). Returns the first match or raises
ValueError if not found.

insert(i, x)

Insert x into the deque at position i.

If the insertion would cause a bounded
deque to grow beyond maxlen, an IndexError is raised.

pop()

Remove and return an element from the right
side of the deque. If no elements are present, raises an IndexError.

popleft()

Remove and return an element from the left
side of the deque. If no elements are present, raises an IndexError.

remove(value)

Remove the first occurrence of value. If
not found, raises a ValueError.

reverse()

Reverse the elements of the deque in-place
and then return None.

rotate(n=1) 循环输出

Rotate the deque n steps to the right. If n
is negative, rotate to the left.

When the deque is not empty, rotating one
step to the right is equivalent to d.appendleft(d.pop()), and rotating one step
to the left is equivalent to d.append(d.popleft()).

Deque objects also provide one read-only
attribute:

maxlen

Maximum size of a deque or None if
unbounded.

1.3.   
ChainMap objects

class collections.ChainMap(*maps)

它是字典的组合,提供的是对字典更高一层的封装/组合。

ChainMap({'Kotlin': 90, 'Python': 86},
{'Go': 93, 'Python': 92}, {'Swift': 89, 'Go': 87})

当在其中引用一个元素时,总是以第一个找到的元素为准。

# chainmap
from collections import ChainMap
# 定义3个dict对象
a = {'Kotlin': 90, 'Python': 86}
b = {'Go': 93, 'Python': 92}
c = {'Swift': 89, 'Go': 87}
# 将3个dict对象链在一起,就像变成了一个大的dict
cm = ChainMap(a, b , c)
print(cm) #
# 获取Kotlin对应的value
print(cm['Kotlin']) # 90
#
获取Python对应的value
print(cm['Python']) # 86
#
获取Go对应的value
print(cm['Go']) # 93

a['Python'] = 45
print(cm['Python']) # 45

pr_type(cm.maps)

属性:

maps:当前类中存在的字典的列表;

new_child(m=None):

以指定的m为首,后序添加cm所拥有的dict

cm = ChainMap(a,b,c)

cm1 = cm.new_child(m={'a':'234','j':23421})
print(cm1) # ChainMap(m,a,b,c)

parents:

等效于ChainMap(*cm1.maps[1:])

cm2 = cm1.parents
print(cm2)

1.4.   
Counter object

class collections.Counter([iterable-or-mapping])

类的主要作用就是字面意思,提供计数功能。

>>>
# Tally occurrences of words in a
list

>>>
cnt = Counter()

>>>
for word in ['red', 'blue', 'red', 'green', 'blue', 'blue']:

...     cnt[word] += 1

>>>
cnt

Counter({'blue':
3, 'red': 2, 'green': 1})

import re
words = re.findall(r'\w+', open('danci.txt',encoding='utf-8').read().lower())
print(len(words))
res = collections.Counter(words).most_common(10)
print(res)

c = Counter()                           # a new,
empty counter
pr_type(c)
c = Counter('gallahad')                 # a new counter from
an iterable
pr_type(c)
c = Counter({'red': 4, 'blue': 2})      # a new counter from
a mapping
pr_type(c)
c = Counter(cats=4, dogs=8)             # a new
counter from keyword args
pr_type(c)

输出:

Counter() <class 'collections.Counter'>

Counter({'a': 3, 'l':
2, 'g': 1, 'h': 1, 'd': 1}) <class 'collections.Counter'>

Counter({'red': 4,
'blue': 2}) <class 'collections.Counter'>

Counter({'dogs': 8,
'cats': 4}) <class 'collections.Counter'>

引用与删除:

引用不存在的元素时会返回0而不是抛出KeyError异常。

Counter objects have a dictionary interface
except that they return a zero count for missing items instead of raising
KeyError:

>>>
c = Counter(['eggs', 'ham'])

>>>
c['bacon']                              # count of a missing element is zero

0

要删除一个元素需要使用del

Setting a count to zero does not remove an
element from a counter. Use del to remove it entirely:

>>>
c['sausage'] = 0                        # counter entry with a zero count

>>>
del c['sausage']

方法:

elements() 返回所有元素,包括重复元素。

Return an iterator over elements repeating
each as many times as its count. Elements are returned in arbitrary order. If
an element’s count is less than one, elements() will ignore it.

>>> c = Counter(a=4, b=2, c=0,
d=-2)

>>> sorted(c.elements())

['a', 'a', 'a', 'a', 'b', 'b']

most_common([n]) 返回重复次数最多的n个元素列表。

Return a list of the n most common elements
and their counts from the most common to the least. If n is omitted or None,
most_common() returns all elements in the counter. Elements with equal counts
are ordered arbitrarily:

>>>
Counter('abracadabra').most_common(3)  #
doctest: +SKIP

[('a', 5), ('r', 2), ('b', 2)]

subtract([iterable-or-mapping])

Elements are subtracted from an iterable or
from another mapping (or counter). Like dict.update() but subtracts counts
instead of replacing them. Both inputs and outputs may be zero or negative.

>>> c = Counter(a=4, b=2, c=0,
d=-2)

>>> d = Counter(a=1, b=2, c=3,
d=4)

>>> c.subtract(d)

>>> c

Counter({'a': 3, 'b': 0, 'c': -3, 'd': -6})

The usual dictionary methods are available
for Counter objects except for two which work differently for counters.

fromkeys(iterable)

This class method is not implemented for
Counter objects.

update([iterable-or-mapping])

Elements are counted from an iterable or
added-in from another mapping (or counter). Like dict.update() but adds counts
instead of replacing them. Also, the iterable is expected to be a sequence of
elements, not a sequence of (key, value) pairs.

Common patterns for working with Counter objects:

sum(c.values())                 # total of all counts

c.clear()                       # reset all counts

list(c)                         # list unique elements

set(c)                          # convert to a set

dict(c)                         # convert to a regular
dictionary

c.items()                       # convert to a list of
(elem, cnt) pairs

Counter(dict(list_of_pairs))    # convert from a list of (elem, cnt) pairs

c.most_common()[:-n-1:-1]       # n least common elements

+c                              # remove zero and negative counts

1.5.   
OrderedDict objects

Ordered
dictionaries are just like regular dictionaries but have some extra
capabilities relating to ordering operations.

dict是无序的,OrderedDict是有序的。

class collections.OrderedDict([items])

popitem(last=True)

The popitem() method for ordered
dictionaries returns and removes a (key, value) pair. The pairs are returned
in LIFO order if last is true or FIFO order if
false.

move_to_end(key, last=True)

Move an existing key to either
end of an ordered dictionary. The item is moved to the right end if last is
true (the default) or to the beginning if last is false. Raises KeyError if
the key does not exist:

>>>
d = OrderedDict.fromkeys('abcde')

>>>
d.move_to_end('b')

>>>
''.join(d.keys())

'acdeb'

>>>
d.move_to_end('b', last=False)

>>>
''.join(d.keys())

'bacde'

1.6.   
defaultdict objects

class collections.defaultdict([default_factory[, ...]])

它就是一个字典的子类,只不过会自动为它的键赋与一个初始值。

def def_value():
    return 90

di = collections.defaultdict(def_value)
di.setdefault(5,9)
print(di['rrt'])
pr_type(di)

__missing__(key)

If the default_factory attribute
is None, this raises a KeyError exception with the key as argument.

If default_factory is
not None, it is called without arguments to provide a default value for
the given key, this value is inserted in the dictionary for the key,
and returned.

接受一个键值,返回默认的value。

>>> di.__missing__(4)

90

default_factory

This attribute is used by the __missing__() method; it
is initialized from the first argument to the constructor, if present, or
to None, if absent.

上例中的def_value就是default_factory,不过一般情况下是int,list等。

笔记-python-standard library-8.3.collections的更多相关文章

  1. Python Standard Library

    Python Standard Library "We'd like to pretend that 'Fredrik' is a role, but even hundreds of vo ...

  2. The Python Standard Library

    The Python Standard Library¶ While The Python Language Reference describes the exact syntax and sema ...

  3. Python语言中对于json数据的编解码——Usage of json a Python standard library

    一.概述 1.1 关于JSON数据格式 JSON (JavaScript Object Notation), specified by RFC 7159 (which obsoletes RFC 46 ...

  4. 《The Python Standard Library》——http模块阅读笔记1

    官方文档:https://docs.python.org/3.5/library/http.html 偷个懒,截图如下: 即,http客户端编程一般用urllib.request库(主要用于“在这复杂 ...

  5. 《The Python Standard Library》——http模块阅读笔记2

    http.server是用来构建HTTP服务器(web服务器)的模块,定义了许多相关的类. 创建及运行服务器的代码一般为: def run(server_class=HTTPServer, handl ...

  6. 《The Python Standard Library》——http模块阅读笔记3

    http.cookies — HTTP state management http.cookies模块定义了一系列类来抽象cookies这个概念,一个HTTP状态管理机制.该模块支持string-on ...

  7. Python Standard Library 学习(一) -- Built-in Functions 内建函数

    内建函数列表 Built-in Functions abs() divmod() input() open() staticmethod() all() enumerate() int() ord() ...

  8. [译]The Python Tutorial#11. Brief Tour of the Standard Library — Part II

    [译]The Python Tutorial#Brief Tour of the Standard Library - Part II 第二部分介绍更多满足专业编程需求的高级模块,这些模块在小型脚本中 ...

  9. C++11新特性——The C++ standard library, 2nd Edition 笔记(一)

    前言 这是我阅读<The C++ standard library, 2nd Edition>所做读书笔记的第一篇.这个系列基本上会以一章一篇的节奏来写,少数以C++03为主的章节会和其它 ...

  10. [译]The Python Tutorial#10. Brief Tour of the Standard Library

    [译]The Python Tutorial#Brief Tour of the Standard Library 10.1 Operating System Interface os模块为与操作系统 ...

随机推荐

  1. vue 实现todolist,包含添加,删除,统计,清空,隐藏功能

    vue 实现todolist,包含添加,删除,统计,清空,隐藏功能 添加:生成列表结构(v-for+数组).获取用户输入(v-model).通过回车新增数据(v-on+.enter) 删除:点击删除指 ...

  2. ubuntu的dpkg命令安装和卸载软件

    实际使用中,可以先到网上下载deb文件,然后用dpkg命令来安装. sudo dpkg -l | grep 360 #查看包含360的软件sudo dpkg -i browser360-cn-stab ...

  3. 每天进步一点点------SOPC的Avalon-MM IP核(一) avalon总线的信号时序

    在SOPC中自定义外设时.可以设置avalon总线的信号时序,以满足外设的要求.一般情况下,可以设为: 其中setup为read和write信号之前,address和writedata信号提前建立的时 ...

  4. yii components文件到底应该放些什么代码

    项目全局用的代码,比如项目所有controller和model的共通操作或者放一些第三方的组件.插件之类的项目全局用的代码

  5. C#面向对象三大特性:封装

    什么是封装 定义:把一个或多个项目封闭在一个物理的或者逻辑的包中.在面向对象程序设计方法论中,封装是为了防止对实现细节的访问. 封装的优点 1. 隔离性,安全性.被封装后的对象(这里的对象是泛指代码的 ...

  6. php 加解密函数

    PHP 加密解密函数: /** * 系统加密方法 * @param string $data 要加密的字符串 * @param string $key 加密密钥 * @param int $expir ...

  7. BFS和DFS详解以及java实现(转载)

    作者:Leo-Yang 原文都先发布在作者个人博客:http://www.leoyang.net/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连 ...

  8. java用JSONObject生成json

    Json在前后台传输中,是使用最多的一种数据类型.json生成的方法有很多,自己只是很皮毛的知道点,用的时候,难免会蒙.现在整理下 第一种: import net.sf.json.JSONArray; ...

  9. touchstart和click 自动区分

    var clickEvent = (function() { if ('ontouchstart' in document.documentElement === true) return 'touc ...

  10. mysql免安装版配置启动时出错

    今天安装了MySQL5.7的免安装版本,启动时报了服务无法启动的错误,在网上找了好久终于找到了解决方法 我找到解决方法的博客地址是:http://blog.csdn.net/qq_27093465/a ...