Python advanced features and python features

Source: Internet
Author: User

Python advanced features and python features

1. advanced python features

1.1 slice

List L = ['mli', 'add', 'sal', 'saoo ', 'lkkl']

L [0: 3] # ['mli', 'add', 'sal'] starts from index 0 and ends with index 3, but does not include 3

L [: 3] is equivalent to L [0: 3]

L [-2:] # ['saoo ', 'lkkl'] the index of the last element is-1.

L [-2:-1] # ['lkkl ']

L = range (100) # Then L [] gets a number for each two elements, so the output is [2, 4]

Tuple can also be used as a slicing operation, but the slicing is not changeable. After the operation, the output result of L [0: 3] IS (1, 2, 3), for example, L = (1, 3)

The string can also be sliced. For example, if m = 'sajlkdjsalkjd ', m [0: 3] outputs 'saj'

1.2 Iteration

If you define a list or tuple, you can use the for loop traversal, called iteration.

For the dict type, you can also use iterations to output key values.

Example: dict = {'lk ': '36', 'klk': '37', 'kkd': '98 '}

For n in dict:

Print n

Result output

Kkd
Lk
Klk

To output the value, you only need

Dict = {'lk ': '36', 'klk': '37', 'kkd': '98 '}

For n in dict. itervalues ():

Print n

Similarly, if you want to output the key-value (both output), you need to change the code to for n in dict. iteritems () # at this time, n is a tuple type.

For key, v in dict. ie = ieritems ()

1.3 list generation

As the name implies, list generative type

[X * x for x in range (3)] # The output result is [0, 1, 4].

[X * x for x in range (3) fi x % 2 = 0] # The output result is [0, 4].

[M + n for m in 'abc' for n in 'abc'] # The output result is ['A', 'AB', 'ac', 'ba ', 'bb', 'bc', 'CA', 'cb ', 'cc'] (in full order)

L = ['abc', 'def', 'ghi'] [s. lower () for s in L] # Replace uppercase letters in L with lowercase letters.

1.4 generator)

When the list builder works, a list must be created. However, the list memory is limited and cannot be infinitely large. To make up for this deficiency, the generator is born naturally, the generator calculates the subsequent results in the process of continuous loops without creating a complete list, so it does not occupy so much memory.

There are many ways to generate a generator: one is to convert the list-generated [] transposition ()

That is, g = (x * x for x in range (5) can only be output using g. next ()

G. next () # output 0

G. next () # output 1

G. next () # output 2

G. next () # output 3

G. next () # output 4

G. next () # output StopIteration

It can also be output cyclically, namely:

For n in g

Print n

Method 2 use yield

For example, the Fibonacci sequence (each number is the sum of the first two numbers), such as, 13 .....

The general function is defined

Def fib (max ):

A, B, n = 0, 0

If n <max:

A, B, n = B, a + B, n + 1

Print

If you use the generator to modify the function, you only need to change print to yield.

 

Def fib (max ):

 

A, B, n = 0, 0

 

If n <max:

 

A, B, n = B, a + B, n + 1

 

Yield

For n in fib (6 ):

Print n

Or o = fib (5)

O. next () for output

Fib is not a common function, but a generator. During the execution process, yield is interrupted. The next time we continue to execute the function, yield will be continuously called during the loop process, and the function will be interrupted. Of course, you must set a condition for the loop to exit the loop. Otherwise, an infinite number of columns will be generated.

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.