Home > Article > Backend Development > Let’s talk about Collections, a built-in module of Python
collections is a built-in module of Python. The so-called built-in module refers to the module that is packaged inside Python and can be used directly without installation. .
Python provides many very easy-to-use basic types, such as the immutable type tuple, which we can easily Use it to represent a binary vector.
namedtuple is a function that creates a custom tuple object and specifies the number of tuple elements, and can use attributes instead of indexes to reference an element of the tuple.
In this way, we can use namedtuple to easily define a data type, which has the invariance of tuple and can be referenced based on attributes, making it very convenient to use.
In this example, we use a three-dimensional coordinate x, y, z to define a tuple object. There are three object elements, and then the corresponding value can be referenced through the coordinate value.
from collections import namedtuple from collections import deque from collections import defaultdict from collections import OrderedDict from collections import Counter def testNamedTuple(): vector=namedtuple('vector',['x','y','z']) flag=vector(3,4,5) print(type(flag)) print(isinstance(flag,vector)) print(isinstance(flag,tuple)) #通过这里的判定我们就可以知晓它是元组类型 print(flag.x,flag.y,flag.z)
deque is a generalized implementation of stack and queue, and deque is the abbreviation of "double-end queue".
Deque supports thread-safe, memory-efficient insertion and deletion of elements at both ends of deque with approximately O(1) performance. Although list also supports similar operations, it is mainly optimized for fixed-length operations. , resulting in a time complexity of O(n) on pop(0) and insert(0,v) (which will change the position and size of the data).
In data structures, we know that queues and stacks are two very important data types, one is first in, first out, and the other is last in, first out.
In python, when using list to store data, accessing elements by index is very fast, but inserting and deleting elements is very slow, because list is linear storage, and when the amount of data is large, the efficiency of insertion and deletion is very low. .
Deque is a doubly linked list structure for efficient implementation of insertion and deletion operations. It is very suitable for implementing data structures such as queues and stacks.
def testDeque(): list1=[x*x for x in range(101)] delist=deque(list1) #对列表进行了一次再处理,让list1列表变成了双向链表结构 delist.append(1000)#将x添加到deque的右侧 delist.appendleft(2000)#将x添加到deque的左侧 delist.pop(1000)#移除和返回deque中最右侧的元素,如果没有元素,将会报出IndexError; delist.popleft()#移除和返回deque中最左侧的元素,如果没有元素,将会报出IndexError; delist.count(1)#返回deque中元素等于1的个数 delist.remove(10000)#移除第一次出现的value,如果没有找到,报出ValueError; delist.reverse()#反转deque中的元素,并返回None; list2=[1,3,4,5] delist.extend(list2)#将可迭代变量iterable中的元素添加至deque的右侧 delist.extendleft(list2)#将变量iterable中的元素添加至deque的左侧,往左侧添加序列的顺序与可迭代变量iterable中的元素相反 delist.maxlen()#只读的属性,deque的最大长度,如果无解,就返回None delist.rotate(1)#从右侧反转n步,如果n为负数,则从左侧反转 delist.clear()#将deque中的元素全部删除,最后长度为0;
defaultdict is a subclass of the built-in data type dict. Its basic functions are the same as dict, except that it overrides a method __missing__(key) and adds a Writable object variable default_factory.
When using the dict dictionary type, if the referenced key does not exist, KeyError will be thrown. If you want a default value to be returned when the Key does not exist, you can use defaultdict.
def testDefaultdict(): dict1= defaultdict(lambda: 'default') #Key不存在时,返回一个默认值,就可以用default,defaultdict的其他行为跟dict是完全一样的 dict1["k1"]="v1" print(dict1["k2"]) list2= [('yellow',11),('blue',2),('yellow',3),('blue',4),('red',5),('red',10)] dict1 = defaultdict(list)#使用list作为default_factory,很容易将一个key-value的序列转换为一个关于list的词典 for k,v in list2: dict1[k].append(v) print(dict1)
OrderedDict is similar to a normal dictionary, except that it remembers the order in which elements are inserted. When iterating over an ordered dictionary, the returned elements are them The order of first addition. In this way dict is an ordered dictionary.
When using dict, keys are unordered. When iterating over a dict, we cannot determine the order of the keys. But if you want to keep the order of keys, you can use OrderedDict.
def testOrderedDict(): dict1=dict([('aaa', 111), ('ddd',444),('bbb', 222), ('ccc', 333)]) print(dict1) dict2 = OrderedDict([('ddd',444),('aaa', 111), ('bbb', 222), ('ccc', 333)])#OrderedDict的key会按照插入的顺序排列,不是key本身排序 print(dict2) dict3 = {"banana": 33, "apple": 222, "pear": 1, "orange": 4444} # dict sorted by key dict4=OrderedDict(sorted(dict3.items(), key=lambda t: t[0])) print("dict4",dict4) # dict sorted by value dict5=OrderedDict(sorted(dict3.items(), key=lambda t: t[1])) print("dict5",dict5) # dict sorted by length of key string dict6 = OrderedDict(sorted(dict3.items(), key=lambda t: len(t[0]))) print("dict6",dict6) print(dict6['apple'])
def testCounter(): '''counter可以支持方便、快速的计数''' str1="abcdefgabcedergeghdjlkabcdefe" #将可迭代的字符串初始化counter str2=Counter(str1) print(str2) #从输出的内容来看,Counter实际上也是dict的一个子类 for k,v in str2.items(): print(k,v) dict3 = {"banana": 33, "apple": 222, "pear": 1, "orange": 4444,"apples":2}#将dict初始化counter dict4=Counter(dict3) print(dict4) print(dict4["test"])#Counter对象类似于字典,如果某个项缺失,会返回0,而不是报出KeyError; dict5=Counter(high=9,age=33,money=-1)#将args初始化counter print(dict5) #elements返回一个迭代器,每个元素重复的次数为它的数目,顺序是任意的顺序,如果一个元素的数目少于1,那么elements()就会忽略它; list1=list(dict5.elements()) print(list1) #most_common返回一个列表,包含counter中n个最大数目的元素 #,如果忽略n或者为None,most_common()将会返回counter中的所有元素,元素有着相同数目的将会以任意顺序排列; str1 = "abcdefgabcedergeghdjlkabcdefe" list1=Counter(str1).most_common(3) print(list1) if __name__ == '__main__': # testNamedTuple() # testCounter() testDefaultdict() # testDeque() # testOrderedDict()
The above is the detailed content of Let’s talk about Collections, a built-in module of Python. For more information, please follow other related articles on the PHP Chinese website!