The Collections library is part of the standard library and contains many data structures, with many modifications and enhancements to lists, dictionaries, and tuples.

Here are some of the most useful ones.

1, deque

It implements a queue that can operate on both ends, equivalent to a double-ended queue, and can specify how many elements to store at most, much like Python’s list of basic data types.

from collections import deque
a = deque(maxlen=3)
Copy the code

The above code defines a two-ended queue of size 3. When you insert a fourth element, the first element of the queue is removed.

a = deque(maxlen=3)
a.append(1) # a = [1]
a.append(2) # a = [1, 2]
a.append(3) # a = [1, 2, 3] FULL
a.append(4) # a = [4] 2
Copy the code

Since this is a double-ended queue, you can insert elements at the beginning of the queue, or delete elements at the beginning and end of the queue, both of which have O(1) time:

  • Append (x) inserts x at the end of the queue
  • Appendleft (x) inserts x in the queue head
  • Pop () removes an element at the end of the queue and returns
  • Popleft () removes an element from the head of the queue and returns
a = deque(maxlen = 10)
a.append(1) # a = [1]
a.append(2) Insert 2 at the end of the queue
a.appendleft(3)Insert 3 at the head of the queue
x = a.pop() # a = [3, 1], x = 2
y = a.popleft() # a = [1], y = 3
Copy the code

2, namedtuple

This library provides named tuples that can be accessed by specified names, for example:

from collections import namedtuple
Point = namedtuple("Point"['x'.'y'.'z'])
p = Point(3.4.5)
print(p.x, p.y, p.z) #Output: 3, 4, 5
Copy the code

The namedTuple function takes the first argument as the name of the new tuple, and the second argument is a mapping of the names of the elements in the tuple. It can be a list of strings, or a string separated by Spaces or commas.

Point = namedtuple("Point"."x y z")
Point = namedtuple("Point"."x,y,z")
Copy the code

It can also be initialized like this, which is very flexible:

p1 = Point(3.4.5)
p2 = Point(x=3, y=4, z=5)
p3 = Point._make([3.4.5])
Copy the code

You can also use namedtuple to set the default value:

PointDef = namedtuple("PointDef"."x, y, z", defaults = [0.0.0])
p = PointDef(x=1) # p is (0, 1)
Copy the code

If you define three names and provide two default values, only the last two will be given default values:

Point = namedtuple("Point"."x y z",defaults ="The [0, 0]) print (Point. _field_defaults) # output: {" y" : 0, "z" : 0}Copy the code

3, Counter

The Counter Counter is useful, especially if you need to count the number of elements in a list or iterable:

from collections importCounter c = Counter (" aaabbccdaaa ")print(c)
#Output: Counter({'a': 6, 'b': 2, 'c': 2, 'd': 1})
Copy the code

You can also easily count the top two frequencies, such as the two most frequent elements:

print(c.most_common(2))
#output: [('a', 6), ('b', 2)]
Copy the code

You can also add and delete strings dynamically, and then count them:

c = Counter("abbc") # {"a":1, "b":2, "c":1}
c.update("bccd") # {"a":1, "b":3, "c":3, "d":1}
c.subtract("bbc") # {"a":1, "b":1, "c":2, "d":1}
Copy the code

4, defaultdict

Defaultdict is similar to dict, but can provide the default data type for dict values, such as:

from collections import defaultdict
toAdd =[("key1".3), ("key2".5), ("key3".6), ("key2".7)]
d = defaultdict(list)
for key, val in toAdd:
  d[key].append(val)
print(d) # {"key1":[3], "key2":[5, 7], "key3":[6]}
Copy the code

If you use dict, you might write:

d = dict(a)for key, val in toAdd:
  if key in d:
    d[key].append(val)
  else:
    d[key] = [val]
Copy the code

Or something like this:

d = dict(a)for key, val in toAdd:
  d.setdefault(key, []).append(val)
Copy the code

In short, DefaultDict is simple and fast.

The last

This article has shared with you four data structures commonly used in the Collections library. Thank you for reading.