Python contains very many standard data structures, such as list,tuple,dict and set, which are built-in types. In addition to these basic built-in types. Python's collections modules are also implemented in a variety of data structures.
Collections----Container data type module
Python version: 2.6 and later versions
The collections module contains data types other than the built-in internal list,dict and tuple.
1.1 Counter
1.1.1 Counter as a container (what is a container, can be loaded into different objects is the container), you can track the number of times the same data.
The initialization input object can be:
1 can be a list
2 Dictionaries
3 Expressions for key parameters
Code:
Import Collectionsprint Collections. Counter (list[' Abcdaa ')) print collections. Counter ({' A ': 1, ' B ': 2, ' C ': 3}) Print collections. Counter (a=2,b=3,c=1)
The result is:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/4B/08/wKiom1Qny3Diql8BAAC7Bsn4-6w079.jpg "Ti Tle= "Collection_counter_init.jpg" alt= "Wkiom1qny3diql8baac7bsn4-6w079.jpg"/>
1.1.2 Constructs a Counter object (this resembles a Dictionary object):
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/4B/08/wKiom1QnzJiwOvkWAAFuFDptfsk550.jpg "Ti Tle= "Colloction_update.jpg" alt= "Wkiom1qnzjiwovkwaafufdptfsk550.jpg"/>
1.1.3 the number of times each element is accessed:
#!/usr/bin/env python#coding=utf8import COLLECTIONSC = collections. Counter (' Abcdaab ') for letter in ' ABCDE ': print '%s:%d '% (Letter,c[letter])
Just remember this counter return is also a dictionary-like object is OK, the other way you can slowly work in the exploration of learning.
This article is from the blog of "Kun-brother", please make sure to keep this source http://kunge.blog.51cto.com/976628/1559092
Python Module learning data structure--collections.counter