5.3.2 Counter Object

Source: Internet
Author: User
Tags iterable

the Counter class provides a convenient and fast statistical tool.

Example:

#python 3.4

Import Collections

CNT = collections. Counter ()

For word in [' Red ', ' blue ', ' red ', ' green ', ' blue ', ' Blue ']:

Cnt[word] + = 1

Print (CNT)

The resulting output is as follows:

Counter ({' Blue ': 3, ' Red ': 2, ' Green ': 1})

From this example, we can see that the statistics of the words, do not need to remember the different words to increase, only call a simple action, you can complete.

Class collections. Counter ([iterable-or-mapping])

the Counter class is an inherited dictionary class and is stored based on HASH . Thus it is a non-ordered container, which treats the element as a key and the count as the key value. The value of the count is an integer, which can be 0 and negative.

When you create a Counter object, you can create it from an iterative object, from a mapped object, or from a Counter object.

Example:

#python 3.4

Import Collections

CNT = collections. Counter ()

Print (CNT)

CNT = collections. Counter (' This was for Test ')

Print (CNT)

CNT = collections. Counter ({' Red ': 4, ' Blue ': 2})

Print (CNT)

CNT = collections. Counter (cats = 4, dogs = 10)

Print (CNT)

The resulting output is as follows:

Counter ()

Counter ({' t ': 3, ' s ': 3, ' ': 3, ' I ': 2, ' R ': 1, ' F ': 1, ' H ': 1, ' O ': 1, ' E ': 1})

Counter ({' Red ': 4, ' Blue ': 2})

Counter ({' Dogs ': Ten, ' Cats ': 4})

There are two points to note when using Counter objects:1. when a non-existent element is accessed, it returns 0 value, rather than throwing an exception. 2. set an element to 0 value, it is not necessary to delete this element, but to call del to remove the element.

Example:

#python 3.4

Import Collections

CNT = collections. Counter ({' Red ': 4, ' Blue ': 2})

Print (CNT)

Print (cnt[' white ')

cnt[' red '] = 0

Print (CNT)

Print (' start Delete:')

Del cnt[' Red ']

Print (CNT)

The resulting output is as follows:

Counter ({' Red ': 4, ' Blue ': 2})

0

Counter ({' Blue ': 2, ' Red ': 0})

Start Delete:

Counter ({' Blue ': 2})

the Counter class adds the following three methods in addition to all of the Dictionary class methods:

Elements ()

Returns an iterator that returns the value of this element as many times as the count of each element. The order in which the elements are returned is random and does not output this element when the value of the element statistic is 0.

Example:

#python 3.4

Import Collections

CNT = collections. Counter (a=4, b=2, c=0, d=-2)

Print (CNT)

Print (List (cnt.elements ()))

The resulting output is as follows:

Counter ({' A ': 4, ' B ': 2, ' C ': 0, ' d ':-2})

[' A ', ' a ', ' a ', ' a ', ' B ', ' B ']

Most_common ([n])

Returns the top n elements with the highest statistic frequency . If n is not specified, all elements are returned. The order in which they are returned is in order from large to small.

Example:

#python 3.4

Import Collections

CNT = collections. Counter (a=4, b=2, c=0, d=-2)

Print (CNT)

Print (Cnt.most_common (2))

The resulting output is as follows:

Counter ({' A ': 4, ' B ': 2, ' C ': 0, ' d ':-2})

[(' A ', 4), (' B ', 2)]

Subtract ([iterable-or-mapping])

The Counter element subtracts the same element from another iteration object or mapping object.

Example:

#python 3.4

Import Collections

Cnt1 = collections. Counter (a=4, b=2, c=0, d=-2)

Cnt2 = collections. Counter (A=1, b=2, c=3, d=4)

Print (CNT1)

Print (CNT2)

Cnt1.subtract (Cnt2)

Print (CNT1)

The resulting output is as follows:

Counter ({' A ': 4, ' B ': 2, ' C ': 0, ' d ':-2})

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

Counter ({' A ': 3, ' B ': 0, ' C ': -3, ' d ':-6})

Fromkeys (iterable)

This method is not implemented in the Counter object.

Update ([iterable-or-mapping])

Adds the statistical value of an element from an iterator or another mapping object.

Example:

#python 3.4

Import Collections

Cnt1 = collections. Counter (a=4, b=2, c=0, d=-2)

Cnt2 = collections. Counter (A=1, b=2, c=3, d=4)

Print (CNT1)

Print (CNT2)

Cnt1.update (Cnt2)

Print (CNT1)

The resulting output is as follows:

Counter ({' A ': 4, ' B ': 2, ' C ': 0, ' d ':-2})

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

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

perform some public operations on the Counter object:

Sum (C.values ()) # calculates all counts.

C.clear () # resets all counts.

List (c) # generate lists

Set (c) # build Collection

Dict (c) # generate a generic dictionary

C.items () # returns a list of pairs (elem, CNT)

Counter (Dict (List_of_pairs)) # Create Counter from the list (Elem, CNT)

C.most_common () [:-n-1:-1] # n most common elements returned

+c # Delete 0 and negative count

arithmetic operations for Counter objects:

#python 3.4

Import Collections

Cnt1 = collections. Counter (a=4, b=2, c=0, d=-2)

Cnt2 = collections. Counter (A=1, b=2, c=3, d=4)

Print (CNT1)

Print (CNT2)

Print (' Cnt1 + cnt2: ', Cnt1 + cnt2)

Print (' Cnt1-cnt2: ', Cnt1-cnt2)

Print (' Cnt1 & Cnt2: ', Cnt1 & Cnt2)

Print (' cnt1 | cnt2: ', Cnt1 | cnt2)

Print (' +cnt1: ', +cnt1)

Print ('-cnt1: ',-cnt1)

The resulting output is as follows:

Counter ({' A ': 4, ' B ': 2, ' C ': 0, ' d ':-2})

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

Cnt1 + cnt2:counter ({' A ': 5, ' B ': 4, ' C ': 3, ' d ': 2})

Cnt1-cnt2:counter ({' A ': 3})

Cnt1 & Cnt2:counter ({' B ': 2, ' A ': 1})

Cnt1 | Cnt2:counter ({' d ': 4, ' A ': 4, ' C ': 3, ' B ': 2})

+cnt1:counter ({' A ': 4, ' B ': 2})

-cnt1:counter ({' d ': 2})



Cai Junsheng qq;9073204 Shenzhen

5.3.2 Counter Object

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.