3 advanced features of Python Learning (Liaoche)

Source: Internet
Author: User
Tags iterable

Advanced Features

One, slice

1, taking a list or a tuple element is a very common operation, Python provides the slice (Slice) operator, can greatly simplify this operation. For example a list:

 L = [ " michael  , "  sarah  , "  tracy  , "  bob  , "  jack   "] 

Take the first three elements: L[0:3] . The output is: ['Michael', 'Sarah', 'Tracy ']

L[0:3] indicates that the fetch starts at index 0 until index 3, but does not include index 3. That is, index 0,1,2, which is exactly 3 elements.

If the first index is 0 , then 0 can be omitted, written as: L[:3] , the output is still ['Michael', 'Sarah ', 'Tracy']

Since Python supports l[-1] to take the first element of the countdown, it also supports reciprocal slices, such as:

>>> l[-2: ['Bob'Jack']> >> l[-2:-1['Bob']

Create a sequence of 0-99:

>>> l[:101, 2, 3, 4, 5, 6, 7, 8, 9]

The first 10 numbers, each 2 fetch 1:

>>> l[:10:22, 4, 6, 8]

All numbers, fetch one per 5:

>>> l[::55, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95]

Write only [:] to copy a list as it is:

>>>1, 2, 3, ..., 99]

2, the tuple is also a list, the only difference is that the tuple is immutable. Therefore, a tuple can also be used for slicing operations, but the result of the operation is still a tuple:

>>> (0, 1, 2, 3, 4, 5) [: 31, 2)

A string ‘xxx‘ can also be seen as a list, with each element being a character. Therefore, a string can also be manipulated with a slice, but the result of the operation is still a string:

' ABCDEFG ' [: 3] ' ABC ' ' ABCDEFG ' [:: 2] ' Aceg '

Second, iterative

In Python, iterations are for ... in done through.

1. Iteration of Dict

(1) By default, the Dict iteration is key.

d={'a': 1,'b': 2,'C': 3}   for in D:    print(i)

The output is:

Abc

(2) to iterate over value, you can use the for value in D.values () :

d={'a': 1,'b': 2,'C': 3}   for in d.values ():    print(i)

The output is:

123

(3) To iterate the key and value at the same time, you can use the for K and V in D.items () :

d={'a': 1,'b': 2,'C': 3}   for in D.items ():    print(i,j)

The output is:

A 123

2, when we use the for loop, as long as the action on an iterative object, the for loop can run normally, and we do not care about whether the object is a list or other data types. For example, a string is also an iterative object:

 for inch ' ABC ' :     Print (a)

The output is:

Abc

3. How can I tell if an object is an iterative object? The method is judged by the iterable type of the collections module:

 from Import iterable Print (Isinstance ('abc', iterable))     # whether the string is an iterative object

The output is: True

4. Python's built-in enumerate function can turn a list into an index-element pair so that both the index and the element itself can be iterated in the For loop:

 for  in Enumerate (['A'B' 'C '  ']):    print(i,value)

The output is:

0 A1 B2 C

Iii. List-Generated

List generation is a simple and powerful build of Python built-in that can be used to create lists.

For example, build list[1,2,3,4,5,6,7,8,9,10] can use list (range (1,11))

Build with List generation[1*1,2*2,3*3,...,10*10]:

 for  in range (1,11)]

The output is: [1, 4, 9, +, +, +, +, +, Bayi, +]

The For loop can also be followed by an if judgment so that we can filter out only the even squares:

 for inch if x% 2 = = 0]

The output is: [4, +, +, +]

3 advanced features of Python Learning (Liaoche)

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.