Note -python-standard library-8.3.collections

Note -python-standard library-8.3.collections

 

1. collections Introduction

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.

Achieve some special container class.

namedtuple()

factory function for creating tuple subclasses with named fields

and

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)

It is mainly used to increase readability, elements can be referenced through the property.


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 created object is a subclass of tuple:

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

True

True

 

1.2. and

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.

It supports thread-safe, memory efficient add and delete, no matter where one end of the operation, are similar to the O (1) operation.

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 is to achieve efficient insertion and deletion of two-way list, queue and stack suitable for:

 

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.

If no parameters maxlen, dqueues Any length; otherwise unlimited length;

If the queue is full exists, inserting a new element means a discarded element from the other end.

 

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) cycle of the output

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)

It is a combination of the dictionary, the dictionary is to provide a higher level of encapsulation / combinations.

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

When an element in which reference is always subject to the elements of the first one found.

# Chainmap
from Collections Import ChainMap
# define three dict objects
A = { 'Kotlin' : 90, 'the Python' : 86}
B = { 'Go' : 93, 'the Python' : 92}
C = { 'Swift' : 89 , 'Go' : 87}
# the three dict objects chained together, becomes just a large dict
cm & lt ChainMap = (a, B, C)
Print (cm & lt) #
# acquired Kotlin corresponding value
Print (cm & lt [ 'Kotlin' ]) # 90
#
acquired Python corresponding value
Print (cm & lt [ 'Python']) # 86
#
acquired Go corresponding value
Print (cm & lt ['Go']) # 93

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

pr_type(cm.maps)

 

Attributes:

maps: a list of dictionaries that exist in the current class;

new_child(m=None):

M led specified, is added after the sequence have dict cm

cm = ChainMap(a,b,c)

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

 

parents:

Equivalent to ChainMap (* cm1.maps [1:])

cm2 = cm1.parents
print(cm2)

 

1.4.    Counter object

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

The main role of the class is the literal meaning, provide a count function.

>>> # 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)

 

Output:

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'>

 

And delete references:

Returns 0:00 referenced element does not exist, rather than throw KeyError exception.

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

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

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

0

To delete an element requires 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']    

 

method:

elements () Returns all the elements, including repetitive 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]) returns the most repeated number n elements list.

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 is disordered, OrderedDict are ordered.

 

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())

'Construction'

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

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

"Bchde '

 

1.6.    defaultdict objects

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

It is a dictionary of subclasses, but it will be automatically assigned to a key initial value.

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.

To accept a key to return to the default 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.

In the example above is def_value default_factory, but under normal circumstances is int, list the like.

 

Guess you like

Origin www.cnblogs.com/wodeboke-y/p/10990383.html