Python module introduction-collections (1) Counter counters

Source: Internet
Author: User
Tags iterable

# Undertake software automation implementation and training and other gtalk: ouyangchongwu # gmail.com qq 37391319 blog: Shenzhen test automation python Project Access Group 113938272 Shenzhen accounting software test part-time job 6089740 # Shenzhen stall group 66250781 wugang cave Chengbu Xinning township sentiment group 49494279 1.3 collections-the main types of container data are as follows: namedtuple (). Create a factory function that contains the subclass of the tuples In the Name field. Python 2.6 is added. Deque: A dual-end queue, similar to a list. both ends of the queue are fast in both stack entry and output. Python 2.4 is added. Counter: a subclass of the dictionary used to calculate hash objects. Python 2.7 is added. OrderedDict: a sub-class of the dictionary, which records the order of addition. Python 2.7 is added. Defaultdict: a subclass of dict. A factory function is called to support non-existent values. Python 2.5 is added. Abstract base classes are also provided to test whether a class provides special interfaces, such as hashing or ing. 1.3.1 Counter (Counter) is a container used to track how many times a value appears. Similar to bag or multiset in other languages. Counters support initialization in three forms. A constructor can call a sequence, including a dictionary of keys and counts, or use a keyword parameter. Importcollections printcollections. counter (['A', 'B', 'C', 'A', 'B', 'B']) printcollections. counter ({'A': 2, 'B': 3, 'C': 1}) printcollections. counter (a = 2, B = 3, c = 1) execution result :#. /collections_counter_init.pyCounter ({'B': 3, 'A': 2, 'C': 1}) Counter ({'B': 3, 'A': 2, 'C': 1}) Counter ({'B': 3, 'A': 2, 'C': 1 }) note that the order in which keys appear is from large to small Based on the count. You can create an empty counter and then update: importcollections c = collections. counter () print 'initial: ', c. update ('abcdeab') print 'sequence: ', c. update ({'A': 1, 'D': 5}) print 'dict: ', c execution result :#. /collections_counter_update.pyInitial: Counter () Sequence: Counter ({'A': 3, 'B': 2, 'C': 1, 'D': 1}) Dict: counter ({'D': 6, 'A': 4, 'B': 2, 'C': 1}) Access count: importcollections c = collections. counter ('abcdeab') forletter in 'abcde': print '% s: % D' % (letter, c [letter]) execution result :#. /collections_counter_get_values.pya: 3b: 2c: 1d: 1e: 0elements can list all elements: importcollections c = collections. counter ('existmely') c ['Z'] = 0 printcprintlist (c. elements () execution result :#. /collections_counter_elements.pyCounter ({'E': 3, 'M': 1, 'L': 1, 'r': 1, 't': 1, 'y': 1, 'X': 1, 'z': 0}) ['E', 'M', 'l', 'R ', 'T', 'y', 'x'] most_common () can extract the most common ones. Importcollections c = collections. counter () withopen ('/usr/share/dict/word', 'rt') as f: for line in f: c. update (line. rstrip (). lower () print 'most common: 'forletter, count in c. most_common (3): print '% s: % 7d' % (letter, count) execution result :#. /collections_counter_most_common.pyMostcommon: e: 484673i: 382454a: 378030 Counter also supports arithmetic and set operations. Both of them only retain the key whose values are positive integers. Importcollections c1 = collections. counter (['A', 'B', 'C', 'A', 'B', 'B']) c2 = collections. counter ('alphabet ') print 'c1:', c1print 'c2: ', C2 print' \ nCombined counts: 'printc1 + c2 print '\ nSubtraction: 'printc1-c2 print' \ nIntersection (taking positive minimums): 'printc1 & c2 print' \ nUnion (taking maximums): 'printc1 | c2 execution result :#. /collections_counter_arithmetic.pyC1: Counter ({'B': 3, 'A': 2, 'C': 1}) C2: Coun Ter ({'A': 2, 'B': 1, 'E': 1, 'H': 1, 'L': 1, 'P': 1, 'T': 1}) Combinedcounts: Counter ({'A': 4, 'B': 4, 'C': 1, 'E': 1, 'H ': 1, 'L': 1, 'P': 1, 't': 1}) Subtraction: Counter ({'B': 2, 'C': 1 }) intersection (taking positive minimums): Counter ({'A': 2, 'B': 1}) Union (taking maximums): Counter ({'B': 3, 'A': 2, 'C': 1, 'E': 1, 'H': 1, 'L': 1, 'P': 1,'t ': 1}) in the preceding example, collections can only process a single character. This is not the case. Please refer to the instances in the standard library. >>> From collections import Counter >>> cnt = Counter () >>> for word in ['red', 'blue', 'red', 'green', 'blue ', 'Blue ']:... cnt [word] + = 1...> cntCounter ({'blue': 3, 'red': 2, 'green': 1}) >>> cnt = Counter (['red', 'blue ', 'red', 'green', 'blue', 'blue']) >>> cntCounter ({'blue': 3, 'red': 2, 'green ': 1}) >>> import re >>> words = re. findall ('\ w +', open ('/etc/ssh/sshd_config '). read (). lower () >>> Counter (words ). mos T_common (10) [('yes', 27), ('no', 23), ('to', 12), (''the, 9 ), ('for', 8), ('and', 8), ('protocol', 6), ('ssh ', 6), ('default', 6 ), ('this', 6)] The Code of section 1st and section 2nd is effective. The code of the subsequent section implements the counting function of simple words through Counter. For example, you can use python to print out the top 10 words in/etc/ssh/sshd_config and their occurrences. Next, let's take a look at the Counter definition: classcollections. Counter ([iterable-or-mapping]). Note that Counter is an unordered dictionary. If the key does not exist, 0.c[ 'sausage'] = 0 is returned. If the value is set to 0, the element is not deleted. Use delc ['sausage']. In addition to the standard dictionary method, the following elements () is added: returns an iterator containing all elements, ignoring the Count smaller than 1. Most_common ([n]): returns the list of the most common elements and their counts. All elements are returned by default. Subtract ([iterable-or-mapping]): subtract. >>> C = Counter (a = 4, B = 2, c = 0, d =-2) >>> d = Counter (a = 1, B = 2, c = 3, d = 4) >>> c-dCounter ({'A': 3}) >>> cCounter ({'A': 4, 'B': 2, 'C': 0, 'D':-2}) >>> dCounter ({'D': 4, 'C': 3, 'B': 2, 'A': 1}) >>> c. subtract (d) >>> cCounter ({'A': 3, 'B': 0, 'C':-3, 'D':-6}) >>> dCounter ({'D': 4, 'C': 3, 'B': 2, 'A': 1 }) from the above we can see that subtract will play a role in the actual Counter, and negative numbers will also be calculated in it. The Standard Dictionary Method, fromkeys is not implemented in Counter. Update is overloaded, and the implementation mechanism is different. Common method: sum (c. values () # total of all countsc. clear () # reset all countslist (c) # list unique elementsset (c) # convert to a setdict (c) # convert to a regulardictionaryc. items () # convert to a list of (elem, cnt) pairsCounter (dict (list_of_pairs) # convert from a list of (elem, cnt) pairsc. most_common () [:-n:-1] # n least common elementsc + = Counter () # remove zero and negativecounts mathematical and intersection, Union: >>> c = Counter (a = 3, B = 1) >>> d = Counter (a = 1, B = 2) >>> c + d # add two counterstogether: c [x] + d [x] Counter ({'A': 4, 'B': 3}) >>> c-d # subtract (keeping onlypositive counts) counter ({'A': 2}) >>> c & d # intersection: min (c [x], d [x]) Counter ({'A': 1, 'B': 1}) >>> c | d # union: max (c [x], d [x]) Counter ({'A': 3, 'B': 2}) description of The operation: The Counter class itself is a dictionary subclasswith no restrictions on its keys and values. the values are intended to benumbers representing counts, but you can store anything in the value field. the most_common () method requires only that the values beorderable. for in-place operations such as c [key] + = 1, the valuetype need only support addition and subtraction. so fractions, floats, anddecimals wocould work and negative values are supported. the same is also truefor update () and subtract () which allow negative and zero values for bothinputs and outputs. the multiset methods are designed only for use cases withpositive values. the inputs may be negative or zero, but only outputs withpositive values are created. there are no type restrictions, but the value typeneeds to support addition, subtraction, and comparison. the elements () method requires integer counts. it ignoreszero and negative counts.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.