Python Advanced Features

Source: Internet
Author: User

1.python Advanced Features

1.1 Slices

List lists l=[' Mli ', ' Add ', ' sal ', ' Saoo ', ' LKKL ']

L[0:3] #即为 [' Mli ', ' Add ', ' Sal '] starting at index 0, ending with index 3, but excluding 3

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

L[-2:] #即为 [' Saoo ', ' LKKL '] The last element is indexed as-1

L[-2:-1] #即为 [' LKKL ']

L=range (+) #则L [1:6:2] takes one number per two elements so the output is [2,4]

A tuple can also be used for slicing, except that slices are immutable, or slices such as l= (1,2,3,4) are L[0:3] output is (

Strings can also be sliced, for example m= ' SAJLKDJSALKJD ' then m[0:3] output ' saj '

1.2 Iterations

If a list or tuple is defined, it can be traversed with a for loop, called an iteration

For dict types, you can also use iterations, except to output a key value

For example: dict={' lk ': ' A ', ' klk ': ' PNs ', ' kkd ': ' 98 '}

For N in Dict:

Print n

Result output

Kkd
Lk
Klk

To output value values simply

dict={' lk ': ' A ', ' klk ': ' PNs ', ' kkd ': ' 98 '}

For N in dict.itervalues ():

Print n

Also, if you want to output the value of the Key-value (two output), you need to change the code to a for n in Dict.iteritems () #此时n为一个tuple类型

For key, V indict.ie=ieritems ()

1.3 List-generated

As the name implies list generation

[X*x for X in range (3)] #输出结果为 [0,1,4]

[X*x for X in range (3) fi x%2==0] #输出结果为 [0,4]

[M+n for M in ' abc ' for n ' abc '] #输出结果为 [' Aa ', ' Ab ', ' Ac ', ' Ba ', ' Bb ', ' Bc ', ' Ca ', ' Cb ', ' Cc '](all arranged)

l=[' ABC ', ' DEF ', ' GHI '] [S.lower () for S in L] #将L中的大写字母改为小写

1.4 Generator (Generator)

When the list Builder is working, a list of lists must be established, but the memory of list is limited, it can't be infinite, in order to make up for this shortcoming, the generator is born naturally, the generator is in the process of continuous cycle to calculate the results of the subsequent, without the need to create a complete list of lists, So you don't use that much memory .

There are many ways to generate a generator, one is to place a list-generated [] transposition ()

i.e.: g= (x*x for x in range (5)) output only with G.next () output

That is G.next () #输出0

G.next () #输出1

G.next () #输出2

G.next () #输出3

G.next () #输出4

G.next () #输出StopIteration

It can also be used as a cyclic output, i.e.:

For N in G

Print n

Yield for two-way use

For example, the Fibonacci sequence (each number is the first two number of the and) example 1,1,2,3,5,8,13 ....

The general function is defined as

def fib (max):

a,b,n=0,1,0

If N<max:

A,b,n=b,a+b,n+1

Print a

If the function is modified with the generator, simply change print to yield

def fib (max):

a,b,n=0,1,0

If N<max:

A,b,n=b,a+b,n+1

    Yield a

Called when for N in Fib (6):

Print n

or O=FIB (5)

O.next () for output

fib is not a normal function, but a generator, in the execution process, encounter yield on the break, the next time we continue to execute, we will continue to call yield during the loop, we are constantly interrupted. of course, you have to set a condition for the loop to exit the loop, or it will produce an infinite sequence.

Python Advanced Features

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.