python collections
namedtuple()
Factory Function for Tuples with Named Fields
They can be used wherever regular tuples are used, and they add the ability to access fields by name instead of position index.
1 >>> # Basic example 2 >>> Point = namedtuple('Point', ['x', 'y'])#每个数据有自己的名字 3 >>> p = Point(11, y=22) # instantiate with positional or keyword arguments 4 >>> p[0] + p[1] # indexable like the plain tuple (11, 22) 5 33 6 >>> x, y = p # unpack like a regular tuple拆包 7 >>> x, y 8 (11, 22) 9 >>> p.x + p.y # fields also accessible by name可以根据名字来访问数据 10 33 11 >>> p # readable __repr__ with a name=value style 12 Point(x=11, y=22)
备注:tuple是不可变的即immutable,具有以下特性:
- 性能优化
- 线程安全
- 可以作为dict的key
- 拆包特性
拆包示例:
c=(1,2,3,4)
b,*c=a
则b=1,c=[2,3,4]
常用方法:
somenamedtuple.
_make
(iterable)
Class method that makes a new instance from an existing sequence or iterable.初始化一个对象,初始化对象的参数必须是可遍历的
>>> t = [11, 22] >>> Point._make(t)#参数是一个可以遍历的数组 Point(x=11, y=22)
somenamedtuple.
_asdict
()
Return a new OrderedDict
which maps field names to their corresponding values:返回一个orderedDict对象
1 >>> p = Point(x=11, y=22) 2 >>> p._asdict() 3 OrderedDict([('x', 11), ('y', 22)])
defaultdict
# -*- coding: utf-8 -*- from collections import defaultdict """ Created on Wed Aug 1 22:19:58 2018 @author: caicai """ #字典常用的统计功能 dict1={} users=['body1','body2','body1','body3','body3','body1'] ''' #写法1: for user in users: if user in dict1: dict1[user]+=1 else: dict[user]=1 #写法2: for user in users: dict1.setdefault(user,0)#当字典里没有这个key时将value默认为0 dict1[user]+=1 print (dict1) ''' #写法3: default_dic=defaultdict(int)#参数为可调用对象,可自定义 for user in users: default_dic[user]+=1 print(default_dic) def aaa(): return{ 'num':0, 'stri':"" } default_dict=defaultdict(aaa) default_dict["groups"]
deque
- class
collections.
deque
([iterable[, maxlen]]) -
Returns a new deque object initialized left-to-right (using
append()
) with data from iterable. If iterable is not specified, the new deque is empty.1 >>> from collections import deque
#初始化一个队列对象 2 >>> d = deque('ghi') # make a new deque with three items 3 >>> for elem in d: # iterate over the deque's elements 4 ... print(elem.upper()) 5 G 6 H 7 I 8 #从右边增加一个条目 9 >>> d.append('j')
#从左边增加一个条目 # add a new entry to the right side 10 >>> d.appendleft('f') # add a new entry to the left side 11 >>> d # show the representation of the deque 12 deque(['f', 'g', 'h', 'i', 'j']) 13 #移除并返回最后一个条目 14 >>> d.pop() # return and remove the rightmost item 15 'j'
#移除并返回最左边的一个条目
16 >>> d.popleft() # return and remove the leftmost item 17 'f' 18 >>> list(d) # list the contents of the deque 19 ['g', 'h', 'i']
#可以按索引来查找所要的条目 20 >>> d[0] # peek at leftmost item 21 'g' 22 >>> d[-1] # peek at rightmost item 23 'i' 24 #倒转一个队列,产生的是一个新的队列对象,d不变 25 >>> list(reversed(d)) # list the contents of a deque in reverse 26 ['i', 'h', 'g'] 27 >>> 'h' in d # search the deque 28 True 29 >>> d.extend('jkl')
#这里增加了jkl后发现ghi的顺序不变 # add multiple elements at once 30 >>> d 31 deque(['g', 'h', 'i', 'j', 'k', 'l']) 32 >>> d.rotate(1) # right rotation 33 >>> d 34 deque(['l', 'g', 'h', 'i', 'j', 'k']) 35 >>> d.rotate(-1) # left rotation 36 >>> d 37 deque(['g', 'h', 'i', 'j', 'k', 'l']) 38 39 >>> deque(reversed(d)) # make a new deque in reverse order 40 deque(['l', 'k', 'j', 'i', 'h', 'g'])
#清空队列 41 >>> d.clear() # empty the deque 42 >>> d.pop() # cannot pop from an empty deque 43 Traceback (most recent call last): 44 File "<pyshell#6>", line 1, in -toplevel- 45 d.pop() 46 IndexError: pop from an empty deque 47 #这个方法将加入的条目倒转了 48 >>> d.extendleft('abc') # extendleft() reverses the input order 49 >>> d 50 deque(['c', 'b', 'a'])备注:deque是线程安全的,list不是线程安全的。
- class
collections.
ChainMap
(*maps)
A ChainMap
groups multiple dicts or other mappings together to create a single, updateable view. If no maps are specified, a single empty dictionary is provided so that a new chain always has at least one mapping.
The underlying mappings are stored in a list. That list is public and can be accessed or updated using the maps attribute. There is no other state.
Lookups search the underlying mappings successively until a key is found. In contrast, writes, updates, and deletions only operate on the first mapping.
A ChainMap
incorporates the underlying mappings by reference. So, if one of the underlying mappings gets updated, those changes will be reflected in ChainMap
.
All of the usual dictionary methods are supported. In addition, there is a maps attribute, a method for creating new subcontexts, and a property for accessing all but the first mapping:
The ChainMap
class only makes updates (writes and deletions) to the first mapping in the chain while lookups will search the full chain. However, if deep writes and deletions are desired, it is easy to make a subclass that updates keys found deeper in the chain:
class DeepChainMap(ChainMap): 'Variant of ChainMap that allows direct updates to inner scopes' def __setitem__(self, key, value): for mapping in self.maps: if key in mapping: mapping[key] = value return self.maps[0][key] = value def __delitem__(self, key): for mapping in self.maps: if key in mapping: del mapping[key] return raise KeyError(key) >>> d = DeepChainMap({'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'}) >>> d['lion'] = 'orange' # update an existing key two levels down >>> d['snake'] = 'red' # new keys get added to the topmost dict >>> del d['elephant'] # remove an existing key one level down DeepChainMap({'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'})
Counter
A counter tool is provided to support convenient and rapid tallies. For example:
>>> # 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}) >>> # Find the ten most common words in Hamlet >>> import re >>> words = re.findall(r'\w+', open('hamlet.txt').read().lower()) >>> Counter(words).most_common(10) [('the', 1143), ('and', 966), ('to', 762), ('of', 669), ('i', 631), ('you', 554), ('a', 546), ('my', 514), ('hamlet', 471), ('in', 451)]