Data structure related modules (list)

Source: Internet
Author: User

All content referenced from Python cookbook

1. Extract sequence assignment to multiple variables in general, our assignment variables are:

a = 1

Python can do this:

a,b = 1,2

or this:

E=[1,2,3,4]a,b,c,d=e

Of course, you want to unzip a few layers to unzip a few layers, such as:

data = [' ACME ', ' 91.1 ', ']name ', ' shares ', ' Price, ' (year, Mon, day) = data

If you just want a few of these variables, this .... Python does not give a special way, you can use some of the non-trivial variable names to occupy a bit. Like what:

data = [' ACME ', ' 91.1 ', ']_ ', shares, price, _ = Data

Although the list is parsed, the same applies to strings, file objects, iterators, generators

s = ' Hello ' a,b,c,d,e = s

Sometimes the elements of an iteration object will exceed the number of variables, so what to do is to use the asterisk expression.

Record = (' Dave ', ' [email protected] ', ' 773-555-1212 ', ' 847-555-1212 ') name, email, *phone_numbers = record

The extracted phone_numbers is always a list, even if there is no element, of course, the more common is to take the asterisk expression as a parameter such as:

def add (*args): n=0 for I in Args:n+=i return n

Then we can pass any parameter to the Add function ~

Add (+/-) Add (5,1,2,6,7)
2. The most frequently occurring elements in a sequence

collections.CounterClasses are designed specifically for this type of problem, and a most_common() way to give answers directly

words = [' look ',  ' to ',  ' my ',  ' eyes ',  ' look ',  ' into ',  ' my ',  ' Eyes ', ' the ',  ' Eyes ',  ' the ',  ' Eyes ',  ' the ',  ' Eyes ',  ' not ',  ' around ',  ' The ', ' Eyes ',  ' don ' t ',  ' look ',  ' around ',  ' the ',  ' Eyes ',  ' look ',  ' into ', ' my ',   ' Eyes ',  "You ' re",  ' under ']from collections import counterword_counts =  counter (words) Top_three = word_counts.most_common (3) (Print (Top_three) # outputs [(' Eyes ',  8),  (' The ',  5),  (' Look ',  4)] 

On the underlying implementation, an Counter object is a dictionary that maps the element to the number of times it appears. Like what:

>>> word_counts[' not ']1

And you can manually increase the count

word_counts[' not ']+=1

CounterInstances can perform mathematical operations


This article is from the "mechanism of small Wind" blog, please be sure to keep this source http://xiaofengfeng.blog.51cto.com/8193303/1885778

Data structure related modules (list)

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.