Python: Counting Statistics with counter

Source: Internet
Author: User
Tags shallow copy

Counting statistics is the number of occurrences of an item. This model is needed for many needs in practical applications. For example, the number of occurrences in a test sample, the frequency of a message appearing in log analysis, and so on, are many implementations of this similar requirement. Here are a few examples.

(1) Using Dict

Look at the code below

#coding =utf-8data = [' A ', ' 2 ', 2,4,5, ' 2 ', ' B ', 4,7, ' a ', 5, ' d ', ' a ', ' z ']count_frq = dict () for one in Data:if one in Count_fr Q:count_frq[one] + = 1 Else:count_frq[one] = 1print Count_frq

The output results are as follows:

{' A ': 3, 2:1, ' B ': 1, 4:2, 5:2, 7:1, ' 2 ': 2, ' Z ': 1, ' d ': 1}

This method is the simplest, but also the most easy to think of, I am writing this blog post before the most, but should not be used in the future, we should make the code more Pythonic

(2) using Set and list

The code is as follows:

#coding =utf-8data = [' A ', ' 2 ', 2,4,5, ' 2 ', ' B ', 4,7, ' a ', 5, ' d ', ' a ', ' z ']data_set = set (data) count_list = []for one in Data_set : Count_list.append ((One,data.count (one))) print Count_list

The output results are as follows:

[(' A ', 3), (2, 1), (' B ', 1), (4, 2), (5, 2), (7, 1), (' 2 ', 2), (' Z ', 1), (' d ', 1)]

This takes advantage of the general method of the list and the properties of the set (set), which is an unordered set of elements, and the factory function set () can convert the list to an unordered set of elements.

The above methods are very simple, but not enough pythonic. The following is an introduction to the Counter class in collections.

A Counter class

The purpose of the counter class is to track the number of occurrences of a value. It is an unordered container type, stored in the form of a dictionary key-value pair, where the element is counted as the key and its count as value. The count value can be any of the interger (including 0 and negative numbers) that supports the set operation + 、-、 &, |, where &, | operations return the maximum and minimum values for each element of the two counter objects, respectively.

(1) Initialization of counter

The initialization method of the custom class is similar to the usual, as follows:

c = Counter ("Hello World") #可迭代对象创建

c = Counter (h=1,l=3,o=2) #关键字创建

c = Counter ({' H ': 1, ' l ': 3, ' O ': 2}) #字典创建

c = Counter () #空Counter类

(2) Common methods of counter class

Elements (): Returns an iterator. How many times the element is repeated, and how many of that element are included in the iterator. All elements are ordered alphabetically, and elements less than 1 are not included.

Update (): For updating the statistics object element, the original counter counter object is added to the statistical count value of the new element instead of being replaced directly.

Subtract (): This method is used to reduce the statistical value of the elements in the Counter object, the input and output of the statistical value of the book can be 0 or negative.

Most_common ([n]): You can find out the top n most frequently occurring elements and their number of times, that is, the frequency of the first line.

Copy (): Shallow copy. For a shallow copy, a deep copy can refer to the previous blog post. http://11026142.blog.51cto.com/11016142/1851472

So the above example with the Counter class, is also very simple, the code is as follows:

#coding =utf-8from Collections Import counterdata = [' A ', ' 2 ', 2,4,5, ' 2 ', ' B ', 4,7, ' a ', 5, ' d ', ' a ', ' z ']c = Counter (data) Print C

The output results are as follows:

Counter ({' A ': 3, 4:2, 5:2, ' 2 ': 2, 2:1, ' B ': 1, 7:1, ' Z ': 1, ' d ': 1})

Let's look at the code.

Print c.elements () Print List (c.elements ())

The output results are as follows:

<itertools.chain Object at 0x7f94b81683d0>

[' A ', ' a ', ' a ', 2, ' B ', 4, 4, 5, 5, 7, ' 2 ', ' 2 ', ' Z ', ' d ']

C[' Z ']-= 1print cprint c.elements () Print List (c.elements ())

The output results are as follows:

Counter ({' A ': 3, 4:2, 5:2, ' 2 ': 2, 2:1, ' B ': 1, 7:1, ' d ': 1, ' Z ': 0})

<itertools.chain Object at 0x7f0e928723d0>

[' A ', ' a ', ' a ', 2, ' B ', 4, 4, 5, 5, 7, ' 2 ', ' 2 ', ' d ']

The value of the element ' Z ' is changed to 0, and then the ' Z ' is eliminated after the elements () operation.

C.update ("AAAA") Print C

Output Result:

Counter ({' A ': 7, 4:2, 5:2, ' 2 ': 2, 2:1, ' B ': 1, 7:1, ' d ': 1, ' Z ': 0})

Update () adds a count value on the original basis

C.subtract ("AAAAA") Print C

The output results are as follows:

Counter ({' A ': 2, 4:2, 5:2, ' 2 ': 2, 2:1, ' B ': 1, 7:1, ' d ': 1, ' Z ': 0})

Subtract () reduce the count value on the original basis

Print C.most_common ()

The output results are as follows:

[(' A ', 2), (4, 2), (5, 2), (' 2 ', 2), (2, 1), (' B ', 1), (7, 1), (' d ', 1), (' Z ', 0)]

The above code is all connected together.

(3) Arithmetic and set operation

#coding =utf-8from Collections Import counterdata = [' A ', ' 2 ', ' 2 ', ' B ', ' A ', ' d ', ' a ',]c = Counter (data) b = Counter (a=1,b=2)    Print Cprint bprint b+c # C[x] + d[x]print c-b # Subtract (element with positive count only) print C&b # intersection: Min (c[x], d[x]) print c|b # and set: Max (C[x], d[x])

The output results are as follows:

Counter ({' A ': 3, ' 2 ': 2, ' B ': 1, ' d ': 1})

Counter ({' B ': 2, ' A ': 1})

Counter ({' A ': 4, ' B ': 3, ' 2 ': 2, ' d ': 1})

Counter ({' A ': 2, ' 2 ': 2, ' d ': 1})

Counter ({' A ': 1, ' B ': 1})

Counter ({' A ': 3, ' 2 ': 2, ' B ': 2, ' d ': 1})


(4) Other

The counter class return value is similar to a dictionary, so the method of the dictionary class applies to the Counter object as well. As follows:

#coding =utf-8from Collections Import counterdata = [' A ', ' 2 ', 2,4,5, ' 2 ', ' B ', 4,7, ' a ', 5, ' d ', ' a ', ' z ']c = Counter (data) Print C.keys () print C.has_key (' a ') print c.get (' a ') print C.items () print c.values () print c.viewitems () print C.viewkeys ( )

The output is as follows:

[' A ', 2, ' B ', 4, 5, 7, ' 2 ', ' Z ', ' d ']

True

3

[(' A ', 3), (2, 1), (' B ', 1), (4, 2), (5, 2), (7, 1), (' 2 ', 2), (' Z ', 1), (' d ', 1)]

[3, 1, 1, 2, 2, 1, 2, 1, 1]

Dict_items ((' A ', 3), (2, 1), (' B ', 1), (4, 2), (5, 2), (7, 1), (' 2 ', 2), (' Z ', 1), (' d ', 1)])

Dict_keys ([' A ', 2, ' B ', 4, 5, 7, ' 2 ', ' Z ', ' d '])

This is only part of the way, and other methods can refer to dictionary classes.

In addition, the Counter object supports the factory function operation set (), List (), Dict ().

Have time to add additional classes in the collections module



Python: Counting Statistics with counter

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.