Python3 Foundation Liao Xuefeng Tutorial notes-3

Source: Internet
Author: User
Tags function definition iterable stdin

Https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/ 0014317568446245b3e1c8837414168bcd2d485e553779e000
In Python, the more code is better, the less the better. The more complex the code is, the better it is, but the simpler the better.
Based on this idea, we introduce the very useful advanced features in Python, the functionality that 1 lines of code can implement,
Never write 5 lines of code. Always keep in mind that the less code you have, the more efficient your development.
Advanced Features
1. Slicing
This operation, which often takes the specified index range, is cumbersome to use with loops, so Python provides a slicing (Slice) operator that can greatly simplify this operation.
>>> L = [' Michael ', ' Sarah ', ' Tracy ', ' Bob ', ' Jack ']

>>> L[0:3]
[' 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, you can also omit the
>>> L[1:3]

L[-1] to the last element of the countdown, it also supports a reciprocal slice
>>> L[-2:]
[' Bob ', ' Jack ']
>>> L[-2:-1]
[' Bob ']

Remember that the index of the first element of the countdown is-1

Easily remove a segment of a sequence by slicing. Like the first 10 numbers.
>>> L = list (range (100))
>>> L[:10]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

10 Number of second:
>>> l[-10:]

Number of first 11-20:
>>> L[10:20]

First 10 numbers, one for every two
>>> L[:10:2]

All numbers, fetch one per 5:
>>> L[::5]


Don't even write anything, just write [:] to copy a list as it is
>>> l[:]

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

The 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 '

A number of various interception functions (for example, substring) are provided for strings, in fact, to slice strings.
Python does not have an intercept function for a string, it can be done simply by slicing one operation.


2. Iteration
Given a list or tuple, we can traverse the list or tuple through a for loop, which we call iteration (iteration).
Iterations are done through for ... in, and in many languages such as C or Java, the iteration list is done by subscript, such as Java code:
for (i=0; i<list.length; i++) {
n = list[i];
}

Because Dict storage is not ordered in list order, the resulting order of the iterations is likely to be different.
By default, the Dict iteration is key.
If you want to iterate over value, you can use the for value in D.values (),
If you want to iterate both key and value at the same time, you can use for K and V in D.items ().

Because a string is also an iterative object, it can also be used for A For loop:
>>> for ch in ' ABC ':
... print (CH)

So, when we use a for loop, the for loop works as long as it works on an iterative object,
And we don't care much about whether the object is a list or another data type.
So, how can you tell if an object is an iterative object? The method is judged by the iterable type of the collections module:
>>> from collections Import iterable
>>> isinstance (' abc ', iterable) # whether STR can iterate
True
>>> ([isinstance], iterable) # Whether the list can be iterated
True
>>> isinstance (123, iterable) # integers can be iterated
False
What if you want to implement subscript loops like Java for a list? Python's built-in enumerate function can turn a list into an index-element pair,
This allows you to iterate both the index and the element itself in the FOR loop:
>>> for I, value in enumerate ([' A ', ' B ', ' C '):
.. print (i, value)
...
0 A
1 B
1 '
The above for loop, which also references two variables, is common in python, such as the following code:
>>> for x, y in [(1, 1), (2, 4), (3, 9)]:
... print (x, y)
...
1 1
2 4
3 9


Summary:
Any iterator object can be used for a for loop, including our custom data types, and can be used for loops as long as the iteration criteria are met.

3. List-Generated
The list generation, which is the comprehensions, is a very simple and powerful build of Python built-in that can be used to create lists.
For example: to generate list [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] You can use a list (range (1, 11)):
>>> list (range (1, 11))
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]


What if you want to generate [1x1, 2x2, 3x3, ..., 10x10]? Method One is the loop:
>>> L = []
>>> for x in range (1, 11):
... L.append (x * x)
...
>>> L
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
But the loop is too cumbersome, and the list generation can use a line of statements instead of loops to generate the list above:
>>> [x * x for x in range (1, 11)]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

When writing list generation, put the element x * x to the front, followed by the For loop, you can create a list, very useful, write a few more times, you will soon be familiar with this syntax.

The For loop can also be followed by an if judgment so that we can filter out only the even squares:
>>> [x * x for x in range (1, one) if x% 2 = = 0]
[4, 16, 36, 64, 100]

You can also use a two-layer loop to generate a full array:
>>> [M + N for m in ' ABC ' for n in ' XYZ ']
[' AX ', ' AY ', ' AZ ', ' BX ', ' by ', ' BZ ', ' CX ', ' CY ', ' CZ ']

Using a list-generated formula, you can write very concise code. For example, listing all the file and directory names in the current directory can be implemented in one line of code:
>>> Import OS # importing OS module, the concept of module is described later
>>> [D for D in Os.listdir ('. ')] # Os.listdir can list files and directories
['. Emacs.d ', '. ssh ', '. Trash ', ' adlm ', ' applications ', ' Desktop ', ' Documents ', ' Downloads ', ' Library ', ' Movies ', ' Music ', ' Pictures ', ' public ', ' VirtualBox VMs ', ' Workspace ', ' XCode '

A For loop can actually use two or more variables at the same time, such as Dict's items () to iterate over both key and value:
>>> d = {' X ': ' A ', ' y ': ' B ', ' z ': ' C '}
>>> for K, V in D.items ():
... print (k, ' = ', V)
...
y = B
x = A
z = C

List generation can also use two variables to generate the list
>>> d = {' X ': ' A ', ' y ': ' B ', ' z ': ' C '}
>>> [k + ' = ' + V for K, V in D.items ()]
[' Y=b ', ' x=a ', ' z=c ']

To turn all the strings in a list into lowercase:
>>> L = [' Hello ', ' World ', ' IBM ', ' Apple ']
>>> [S.lower () for s in L]
[' Hello ', ' World ', ' IBM ', ' Apple ']


4. Generator
With list generation, we can create a list directly. However, with memory limitations, the list capacity is certainly limited.
And, creating a list of 1 million elements that not only takes up a lot of storage space, if we just need to access the first few elements,
The vast majority of the elements behind that space are wasted.

If a list element can be inferred from an algorithm, can we continually extrapolate the subsequent elements in the process of looping?
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.

There are a number of ways to create a generator.

The first method is simple, as long as a list-generated [] is changed to (), a generator is created:
>>> L = [x * x for x in range (10)]
>>> L
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
>>> g = (x * x for x in range (10))
>>> g
<generator Object <genexpr> at 0x1022ef630>

The difference between creating L and G is only the outermost [] and (), L is a list, and G is a generator.
If you want to print one, you can get the next return value of generator with the next () function
>>> Next (g)
0
>>> Next (g)
1
>>> Next (g)
4
>>> Next (g)
9
>>> Next (g)
16
>>> Next (g)
25
>>> Next (g)
36
>>> Next (g)
49
>>> Next (g)
64
>>> Next (g)
81
>>> Next (g)
Traceback (most recent):
File "<stdin>", line 1, in <module>
Stopiteration

Generator saves the algorithm, each time it calls next (g), it calculates the value of the next element of G until it is calculated to the last element,
When there are no more elements, throw stopiteration errors.

Of course, this constant invocation of next (g) is so perverted that the correct approach is to use a For loop because generator is also an iterative object:

>>> g = (x * x for x in range (10))
>>> for N in G:
... print (n)
...
0
1
4
9
16
25
36
49
64
8
After we create a generator, we basically never call next (), but instead iterate over it with a for loop and don't need to care about Stopiteration's error.


Another way to define generator.
If a function definition contains the yield keyword, then the function is no longer a normal function, but a generator:
def fib (max):
N, a, b = 0, 0, 1
While n < max:
Yield b
A, B = B, A + b
n = n + 1
Return ' Done '

The most difficult thing to understand here is that the generator is not the same as the execution flow of the function.
The function is executed sequentially, the return statement is encountered, or the last line function statement is returned. and become a function of generator,
Executes at each call to next (), encounters a yield statement return, and executes again from the yield statement that was last returned.
To give a simple example, define a generator and return the number 1,3,5 in turn:
Def odd ():
Print (' Step 1 ')
Yield 1
Print (' Step 2 ')
Yield (3)
Print (' Step 3 ')
Yield (5)
When you call the generator, you first generate a generator object and then use the next () function to continuously get the next return value:
>>> o = Odd ()
>>> Next (O)
Step 1
1
>>> Next (O)
Step 2
3
>>> Next (O)
Step 3
5
>>> Next (O)
Traceback (most recent):
File "<stdin>", line 1, in <module>
Stopiteration


Summary
Generator is a very powerful tool, in Python, you can simply change the list generation to generator, or you can implement the generator of complex logic through functions.

To understand how generator works, it is continuously calculating the next element during the for loop and ending the For loop in the appropriate condition. For the generator of a function, a return statement or execution to the last line of the function body is the end of the generator instruction, and the For loop ends with it.

Notice that the normal function and the generator function are distinguished, and the ordinary functions call to return the result directly:

>>> r = ABS (6)
>>> R
6
The "call" of the generator function actually returns a generator object:

>>> g = fib (6)
>>> g
<generator Object fib at 0x1022ef948>

5. iterators
We already know that there are several types of data that can be directly applied to a For loop:
A class is a collection of data types, such as list, tuple, dict, set, str, and so on;
A class is generator, including generators and generator function with yield.

These objects that can directly act on a for loop are called an iterative object: Iterable.

You can use Isinstance () to determine whether an object is a Iterable object:
>>> from collections Import iterable
>>> isinstance ([], iterable)
True
>>> isinstance ({}, iterable)
True
>>> isinstance (' abc ', iterable)
True
>>> Isinstance ((x for X in range), iterable)
True
>>> isinstance (iterable)
False

The generator can not only be used for a for loop, but it can also be called by the next () function and return the next value until the last throw Stopiteration error indicates that the next value cannot be returned again.

An object that can be called by the next () function and continually returns the next value is called an iterator: Iterator.

You can use Isinstance () to determine whether an object is a iterator object:

>>> from collections Import Iterator
>>> Isinstance ((x for X in range), Iterator)
True
>>> isinstance ([], Iterator)
False
>>> isinstance ({}, Iterator)
False
>>> isinstance (' abc ', Iterator)
False

Generators are iterator objects, but list, dict, and Str are iterable, but not iterator.
To change the list, dict, str, etc. iterable to iterator you can use the ITER () function:
>>> Isinstance (ITER ([]), Iterator)
True
>>> isinstance (ITER (' abc '), Iterator)
True

You might ask, why are data types such as list, dict, and str not iterator?
This is because the Python iterator object represents a data stream, and the iterator object can be called by the next () function and continuously return to the next data.
The Stopiteration error is thrown until there is no data. This data stream can be viewed as an ordered sequence, but we cannot know in advance the length of the sequence
The next data is calculated on demand by the next () function, so the calculation of the iterator is inert, only if the next data needs to be returned
When it is calculated.

Iterator can even represent an infinitely large stream of data, such as the whole natural number. Using list is never possible to store all natural numbers.

Summary
All objects that can be used for a for loop are iterable types;
All objects that can be used for the next () function are iterator types, which represent a sequence of lazy computations;
Collection data types such as list, Dict, str, and so on are iterable but not iterator, but a iterator object can be obtained through the ITER () function.
Python's for loop is essentially implemented by constantly invoking the next () function, for example:
For x in [1, 2, 3, 4, 5]:
Pass
is actually exactly equivalent to:

# Get the Iterator object first:
it = iter ([1, 2, 3, 4, 5])
Cycle
While True:
Try
# Get the next value:
x = Next (IT)
Except stopiteration:
# exit the loop when you encounter stopiteration
Break


Python3 Foundation Liao Xuefeng Tutorial notes-3

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.