Liaoche Python excerpt 4

Source: Internet
Author: User

1. The object that directly acts on for the loop is called an iterative object:Iterable。    可以被next()函数调用并不断返回下一个值的对象称为迭代器:Iterator

2, generators are Iterator objects, but,, list dict str Although Yes Iterable , but not Iterator .

Turn list , dict wait, and str Iterable turn into Iterator functions that can be used iter()

3. Because the Python Iterator object represents a data stream, the iterator object can be called by the next() function and will return the next data continuously until there is no data to throw an StopIteration error. You can think of this data stream as an ordered sequence, but we can't know the length of the sequence in advance, only by continuously using the next() function to calculate the next data on demand, so Iterator the calculation is lazy, and it will only be calculated when the next data needs to be returned.

4, we can break the complex task into a simple task by splitting the large segment code into a function through a layer of function call, which can be called process-oriented programming. function is the basic unit of process-oriented program design.

5. One of the features of functional programming is that it allows the function itself to be passed as a parameter to another function, and also allows a function to be returned!

Python provides partial support for functional programming. Because Python allows the use of variables, Python is not a purely functional programming language.

6, the higher order function English is called Higher-order functions.

(1) Variable can point to function 1 >>> f = abs 2 >>> f ( -10) 3 Description variable fNow it's pointing to the absfunction itself. Call directly abs()Functions and call variables f()Exactly the same

(2) abs after pointing 10 , it cannot be abs(-10) called by the function! Because abs this variable does not point to an absolute value function but points to an integer 10 !

1 >>> abs =2 >>> abs ( -10)3Traceback (most recent call last) :4   "<stdin>" in <module>5'  int' is not callable

7, we look at map first. The map() function receives two arguments, one is the function, the other is to function the Iterable map incoming function to each element of the sequence sequentially, and returns the result as a new one Iterator .

8. Thereduce () function is also a high-order function built into Python.

The reduce () function receives a parameter similar to map (), a function f, a list, but behaves differently from map (), and reduce () the incoming function f must receive two parameters, and reduce () calls function f repeatedly on each element of the list, and returns the final result value. Again look at reduce the usage. To function reduce on a sequence [x1, x2, x3, ...] , the function must receive two parameters, reduce and the result continues and the next element of the sequence is accumulated

9.

1 >>> from Functools import Reduce2 >>> def fn (x, y): 3 ...     return x * + Y4 ... 5 >>> def char2num (s): 6 ...     return {' 0 ': 0, ' 1 ': 1, ' 2 ': 2, ' 3 ': 3, ' 4 ': 4, ' 5 ': 5, ' 6 ': 6, ' 7 ': 7, ' 8 ': 9}[s]7 ... 8 >>> Reduce (FN, map (char2num, ' 13579 ')) 9 13579

The Char2num () in turn converts the input single str ' 1 ' to 1

10 (example), the use map() of functions, the user entered the non-standard English name, the first letter capitalized, other lowercase canonical name. Input: [‘adam‘, ‘LISA‘, ‘barT‘] , output: [‘Adam‘, ‘Lisa‘, ‘Bart‘] :

List (Map (lambda x:x.capitalize (), [' Mike ', ' Jack '])

map()The first parameter passed in is the f function object itself. Since the result r is one Iterator , it is an Iterator inert sequence, so the list() function allows it to calculate the entire sequence and return a list.

Feel lambda is used more widely, not only abs (), but also x.capitalize ()

11, and map() similar, filter() also receive a function and a sequence. And the map() difference is that the filter() incoming function acts on each element in turn, and then whether True False the element is persisted or discarded based on the return value.

12, in addition, the sorted() function is also a high-order function, it can also receive a key function to implement a custom sort, for example, by the absolute size of the order

13. The higher order function can also return the function as the result value, in addition to being able to accept the function as a parameter.

14, we define the function in the function lazy_sum sum , and the intrinsic function sum can refer to lazy_sum the parameters and local variables of the external function, when lazy_sum returning sum the function, the relevant parameters and variables are saved in the returned function, this is called "   The program structure of the closure (Closure) has great power. When returning a function, keep in mind that the function is not executed, and do not refer to any variables that may change in the return function.

Liaoche Python excerpt 4

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.