List-generated Python

Source: Internet
Author: User

One, R's prevent character escaping

print R"a\nb"

Operation Result:

A\nb

Second, get the variable type

A = r"a\nb"print type (a)

Operation Result:

' Str '>

Iii. Types of Judgments

A = r"a\nb"print isinstance (A,STR)

Operation Result:

True

Iv. iterating over list, tuple, dict, set

4.1 Common ways

list = ['a','b','C']  for inch list:     Print I

Dict = {"K1":"v1","K2":"  v2"} for in Dict.items ():    Print k,v

But this iterative approach will load the list into memory for iteration

4.2 Using iterators to iterate

list = ['a','b','C']  for inch iter (list):     Print I

 dict = { k1  : "  v1  , "  k2  : "  v2   "   for  k,v in   Dict.iteritems ():  print  k,v 
Dict = {"K1":"v1","K2":"  v2"} for in Dict.iterkeys ():      Print K

The iterative comparison of this approach saves memory

Iteration subscript at the same time as the 4.3 iteration value

list = ['a','b','C']  for inch Enumerate (list):     Print Index,value

V. List-Generated

Generate a list from a collection

List1 = ['a','b','C'= [ "printfor"inifand Y<3]

The operating result is:

['aa'bb'cc']

Vi. List Generator

With list generation, we can create a list directly. However, with memory limitations, the list capacity is certainly limited. Also, creating a list of 1 million elements takes up a lot of storage space, and if we just need to access the first few elements, the vast majority of the space behind it is wasted.
So, if the list element can be calculated according to an algorithm, can we continue to calculate the subsequent elements in the process of the loop? This eliminates the need to create a complete list, which saves a lot of space. In Python, this side loop computes the mechanism, called the generator: Generator.

As soon as a list-generated [] is changed to (), a generator is created:
g = (x * x for x in range (10))
Generator saves the algorithm, each time it calls next (g), calculates the value of the next element of G, until the last element is calculated, and when there are no more elements, the Stopiteration error is thrown.
But we usually iterate over it with a for loop, and we don't need to care about Stopiteration's error.
For N in G:
Print (n)

List-generated Python

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.