Python Module learning data structure--collections.counter

Source: Internet
Author: User

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

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.