Collection module,
1. nametuple -- factory function for creating tuple subclasses with named fields
Create a data type similar to the parent database. In addition to being able to access data using indexes, data can be iterated, and data can be accessed using attribute names conveniently.
Example:
From collections import namedtuple Friend = namedtuple ("Friend", ['name', 'age', 'email ']) # defines a class, the class has three attributes: f1 = Friend ('xiaowang ', 33, 'xiaowang @ 163.com') # create an object print (f1) print (f1.age) print (f1.email) f2 = Friend (name = 'xiaozhang', email = 'xiaozhang @ sina.com ', age = 30) print (f2) name, age, email = f2 # print (name, age, email) of the unwrapped tuples)
However, it keeps the elements of the tuples unmodifiable!
f1.age = 25Traceback (most recent call last): File "<stdin>", line 1, in <module>AttributeError: can't set attribute
Process the nametuple object:
From collections import namedtupleFriend = namedtuple ("Friend", ['name', 'age', 'email ']) # defines a class, the class has three attributes: f1 = Friend ('xiaowang ', 33, 'xiaowang @ 163.com') # create an object f2 = Friend (name = 'xiaozhang ', email = 'xiaozhang @ sina.com ', age = 30) name, age, email = f2print (f1. _ asdict () # convert to OrderedDict ([('name ', 'xiaowang '), ('age', 33), ('email', 'xiaowang @ 163.com ')]) print (dict (f1. _ asdict ())) # Then it can be converted to the dictionary print (f1. _ make (['Alex ', '2017 @ 222.com', 40]) # Friend (name = 'Alex ', age = '1970 @ 222.com ', email = 40)
If you want to modify the attribute value of a nametuple object:
From collections import namedtupleFriend = namedtuple ("Friend", ['name', 'age', 'email ']) # defines a class, the class has three attributes: f1 = Friend ('xiaowang ', 33, 'xiaowang @ 163.com') # create an object f2 = Friend (name = 'xiaozhang ', email = 'xiaozhang @ sina.com ', age = 30) print (f1. _ replace (name = 'shangsan '))
def namedtuple(typename, field_names, *, verbose=False, rename=False, module=None): """Returns a new subclass of tuple with named fields. """ if isinstance(field_names, str): field_names = field_names.replace(',', ' ').split() field_names = list(map(str, field_names)) typename = str(typename) if rename: seen = set() for index, name in enumerate(field_names): if (not name.isidentifier() or _iskeyword(name) or name.startswith('_') or name in seen): field_names[index] = '_%d' % index seen.add(name) for name in [typename] + field_names: if type(name) is not str: raise TypeError('Type names and field names must be strings') if not name.isidentifier(): raise ValueError('Type names and field names must be valid ' 'identifiers: %r' % name) if _iskeyword(name): raise ValueError('Type names and field names cannot be a ' 'keyword: %r' % name) seen = set() for name in field_names: if name.startswith('_') and not rename: raise ValueError('Field names cannot start with an underscore: ' '%r' % name) if name in seen: raise ValueError('Encountered duplicate field name: %r' % name) seen.add(name) # Fill-in the class template class_definition = _class_template.format( typename = typename, field_names = tuple(field_names), num_fields = len(field_names), arg_list = repr(tuple(field_names)).replace("'", "")[1:-1], repr_fmt = ', '.join(_repr_template.format(name=name) for name in field_names), field_defs = '\n'.join(_field_template.format(index=index, name=name) for index, name in enumerate(field_names)) ) namespace = dict(__name__='namedtuple_%s' % typename) exec(class_definition, namespace) result = namespace[typename] result._source = class_definition if verbose: print(result._source) if module is None: try: module = _sys._getframe(1).f_globals.get('__name__', '__main__') except (AttributeError, ValueError): pass if module is not None: result.__module__ = module return result
Ii. deque-list-like container with fast appends and pops on either end
Deque is the abbreviation of double-ended queue. It is similar to list, but it provides insert and delete operations on both ends.
queue = deque()# append values to wait for processingqueue.appendleft("first")queue.appendleft("second")queue.appendleft("third")# pop values when readyprocess(queue.pop()) # would process "first"# add values while processingqueue.appendleft("fourth")# what does the queue look like now?queue # deque(['fourth', 'third', 'second'])
1. append/appendleft/extend/extendleft
from collections import collectionsd1 = collections.deque()d1.extend('abcdefg')print 'extend:', d1d1.append('h')print 'append:', d1d2 = collections.deque()d2.extendleft(xrange(6))print 'extendleft', d2d2.appendleft(6)print 'appendleft', d2
2. pop/popleft
from collections import collectionsprint "From the right:"d = collections.deque('abcdefg')while True: try: print d.pop(), except IndexError: breakprintprint "\nFrom the left:"d = collections.deque(xrange(6))while True: try: print d.popleft(), except IndexError: breakprint
Because the dual-end queue is thread-safe, the queue content can be used from both ends in different threads at the same time.
import collectionsimport threadingimport timecandle = collections.deque(xrange(5))def burn(direction, nextSource): while True: try: next = nextSource() except IndexError: break else: print '%8s: %s' % (direction, next) time.sleep(0.1) print '%8s done' % direction returnleft = threading.Thread(target=burn, args=('Left', candle.popleft))right = threading.Thread(target=burn, args=('Right', candle.pop))left.start()right.start()left.join()right.join()
3. rotate
import collectionsd = collections.deque(xrange(10))print 'Normal:', dd= collections.deque(xrange(10))d.rotate(2)print 'Right roration:', dd = collections.deque(xrange(10))d.rotate(-2)print 'Left roration:', d
Iii. Counter-dict subclass for counting hashable objects
The Counter class is used to track the number of occurrences of a value. It is an unordered container type, which is stored in the form of dictionary key-value pairs, where elements are used as keys and Their counts are used as values.
1. Create
From collections import Counterc1 = Counter () # create an empty Counterc2 = Counter ('Glad to see you') c3 = Counter ([,]) c4 = Counter ({'A': 4, "B": 2,}) c5 = Counter (a = 4, B = 2)
2. Access to the Count value and missing keys
If the accessed key does not exist, 0 instead of KeyError is returned; otherwise, its count is returned.
from collections import Counterc = Counter('glad to see you')print(c['t']) # 1print(c['h']) # 0
3. update the counter (update and subtract)
You can use one iterable object or another Counter object to update the key value.
Counter updates include increase and decrease: increase (update) and decrease (subtract)
Add operation:
From collections import Counterc = Counter ('Glad to see you') d = Counter ('me too') c. update (d) c. update ([1, 2, 3, 4]) # update can be any iteratable object print (c)
Reduce operation:
from collections import Counterc = Counter('glad to see you')d = Counter('me too')c.subtract(d)c.subtract([1,2,3,4])print(c)#############Counter({' ': 2, 'g': 1, 'l': 1, 'a': 1, 'd': 1, 's': 1, 'e': 1, 'y': 1, 'u': 1, 't': 0, 'o': 0, 'm': -1, 1: -1, 2: -1, 3: -1, 4: -1})
4. Deletion of keys
When the Count value is 0, it does not mean that the element is deleted. del should be used to delete the data.
from collections import Counterc = Counter('glad to see you')del c['g']print(c)
5. elements ()
Returns an iterator. The number of times that an element has been repeated. All elements are sorted alphabetically. Elements smaller than 1 are not included.
From collections import Counterc = Counter ('gladtoseeyou') print (list (c) # retrieve the key value print (list (c. elements () # obtain the key value print (list (sorted (c. elements () # Sort key values
6. most_common (N)
Returns a TopN list. If n is not specified, all elements are returned. When the Count values of multiple elements are the same, they are sorted alphabetically.
From collections import Counterc = Counter ('gladtoseeyou') print (c. most_common (3) # extract the first three elements (self, n = None) that appear most frequently from the smallest to the smallest ): '''list the n most common elements and their counts from the most common to the least. if n is None, then list all element counts. >>> counter ('abcdeabcdabcaba '). most_common (3) [('A', 5), ('B', 4), ('C', 3)] ''' if n is None: return sorted (self. items (), key = _ itemgetter (1), reverse = True) return _ heapq. nlargest (n, self. items (), key = _ itemgetter (1 ))
7. arithmetic and Set Operations
+,-, &, | Operations can also be used for Counter. The & | operation returns the minimum and maximum values of each element in two Counter objects.
Note that the obtained Counter object will delete elements smaller than 1.
From collections import Counterc = Counter (a = 1, B = 2, c = 3) d = Counter (a = 4, B = 5) print (c + d) print (d-c) print (c-d) # Note: delete a value smaller than the negative number print (c & d) # equivalent to min (c [x], d [x]) print (c | d) # equivalent to max (c [x], d [x])
8. Common Operations
From collections import Counterc = Counter ("fasdfladfsdasdfsadfadsfwefadscvcxhaegdflffadsfwefnlsdfwefojcc") sum (c. values () # The total number of all counts c. clear () # reset the Counter object, note not to delete list (c) # convert the key in c to the list set (c) # convert the key in c to setdict (c) # convert the key-value pairs in c to the dictionary list_of_pairs = c. items () # convert to (elem, cnt) format list Counter (dict (list_of_pairs) # convert from (elem, cnt) format list to Counter Class Object c. most_common () [:-5:-1] # retrieve the minimum count of five elements c ++ = Counter () # Remove 0 and negative values
4. OrderedDict-dict subclass that remembers the order entries were added
The OrderedDict type is an ordered dictionary, which is actually an order more than a common dictionary.
Import collectionsdic = collections. orderedDict () dic ["k1"] = "v1" dic ["k2"] = "v2" dic ["k3"] = "v3" print (dic) # implementation principle: It is equivalent to maintaining dictionary (unordered) sorting by list (ordered). The following is only for understanding # dic = {"k1": "v1", "k2 ": "v2"} # li = ["k1", "k2"] # for I in li: # print (dic. get (I) execution result: no matter how many times the execution result is the same as OrderedDict ([('k1 ', 'v1'), ('k2', 'v2 '), ('k3 ', 'v3')])
# Orderly Delete and specify to delete import collectionsdic = collections. orderedDict () dic ["k1"] = "v1" dic ["k2"] = "v2" dic ["k3"] = "v3" print (dic) dic. popitem () # remove the last one in an orderly manner, which is equivalent to storing the memory stack, followed by the first-in-first-out principle, and pop () is to force the execution result of the specified print (dic) value: orderedDict ([('k1 ', 'v1'), ('k2', 'v2'), ('k3 ', 'v3')]) orderedDict ([('k1 ', 'v1'), ('k2', 'v2')])
# Move the specified key value to the last import collectionsdic = collections. orderedDict () dic ["k1"] = "v1" dic ["k2"] = "v2" dic ["k3"] = "v3" print (dic) dic. move_to_end ("k1") # Move the specified key value to the final print (dic) # execution result: OrderedDict ([('k1 ', 'v1'), ('k2 ', 'v2'), ('k3 ', 'v3')]) OrderedDict ([('k2', 'v2'), ('k3 ', 'v3 '), ('k1 ', 'v1')])
# Add the default key import collectionsdic = collections. orderedDict () dic ["k1"] = "v1" dic ["k2"] = "v2" dic ["k3"] = "v3" print (dic) dic. setdefault ("k4", "v4") # The default key value is None, but the print (dic) value can be added. # execution result: OrderedDict ([('k1 ', 'v1 '), ('k2', 'v2'), ('k3', 'v3 ')]) OrderedDict ([('k1', 'v1 '), ('k2', 'v2'), ('k3 ', 'v3'), ('k4 ', 'v4')])
Note: The OD order is actually implemented by a two-way linked list. Since list in Python is a mutable object, PREV and NEXT in a node list are references to the list of the precursor and successor nodes. Where, last = root [0] last [1] = root [0] = self. _ map [key] = [last, root, key], to insert a node before the root.
V. defaultDict-dict subclass that CILS a factory function to supply missing values
An empty dictionary cannot be set by a key before initialization. If you try to forcibly set the value, an exception is reported. The default dictionary is used to solve this problem.
The following is an example:
123 |
frequencies = {} for word in wordlist: frequencies[word] + = 1 |
Python will throw a KeyError exception, because the dictionary index must be initialized before, you can solve it using the following method:
From collections import defadicdictfrequencies = defaultdict (int) # Pass in the int () function to initialize for word in wordlist: frequencies [word] + = 1
Collections. defaultdict can be initialized using a function as a parameter.