1, first see what is the Iterable object
Take the built-in Max function as an example to view its doc:
Copy Code code as follows:
>>> Print max.__doc__
Max (iterable[, Key=func])-> value
Max (A, B, C, ...) [, Key=func]) -> value
With a single iterable argument, return to its largest item.
With two or more arguments, return the largest argument.
In the first form of the Max function, the first argument is a Iterable object, so what are the Iterable objects?
Copy Code code as follows:
>>> Max (' Abcx ')
>>> ' x '
>>> Max (' 1234 ')
>>> ' 4 '
>>> Max ((1,2,3))
>>> 3
>>> Max ([1,2,4])
>>> 4
We can use yield to generate a Iterable object (there are other ways):
Copy Code code as follows:
def my_range (start,end):
''' '''
While the start <= end:
Yield start
Start = 1
Execute the following code:
Copy Code code as follows:
For num in My_Range (1, 4):
Print num
Print Max (My_Range (1, 4))
Will output:
Copy Code code as follows:
2, Map
The map function is described so in Http://docs.python.org/2/library/functions.html#map:
Copy Code code as follows:
Map (function, iterable, ...)
Apply function to every item of iterable and return a list of the results. If additional iterable arguments are passed, function must take that many arguments and are applied to the Terables in parallel. If one iterable is shorter than another it are assumed to being extended with None items. The If function is None, the identity function is assumed; If there are multiple arguments, map () returns a list consisting of tuples containing the corresponding items from all ITE Rables (a kind of transpose operation). The iterable arguments may is a sequence or any iterable object; The result is always a list.
The map function uses a custom function to process each element in the iterable, returning all the processing results as a list. For example:
Copy Code code as follows:
def func (x):
''' '''
Return x*x
Print Map (func, [1,2,4,8])
Print Map (func, My_Range (1, 4))
The results of the operation are:
Copy Code code as follows:
[1, 4, 16, 64]
[1, 4, 9, 16]
It can also be achieved by list derivation:
Copy Code code as follows:
print [x*x for x in [1,2,4,8]]
3. Reduce
The reduce function is described as follows in Http://docs.python.org/2/library/functions.html#reduce:
Copy Code code as follows:
Reduce (function, iterable[, initializer])
Apply function of two arguments cumulatively to the ' items of iterable, from left to right, so as to reduce the iterable to A single value. For example, reduce (lambda x, Y:x+y, [1, 2, 3, 4, 5]) calculates ((((1+2) +3) +4). The left argument, X, are the accumulated value and the right argument, y, are the update value from the iterable. If the optional initializer is present, it's placed before the items of the iterable in the calculation, and serves as a Default when the iterable is empty. If initializer is isn't given and Iterable contains only one item, the ' the ' is returned.
This has been introduced very clearly,
Copy Code code as follows:
Reduce (lambda x, Y:x+y, [1, 2, 3, 4, 5])
Equivalent to the calculation
Copy Code code as follows:
and
Copy Code code as follows:
Reduce (lambda x, Y:x+y, [1, 2, 3, 4, 5],6)
Equivalent to the calculation
Copy Code code as follows:
((((((6+1) +2) +3) +4) +5)
4, Filter
The filter function is described in Http://docs.python.org/2/library/functions.html#filter as follows:
Copy Code code as follows:
Filter (function, iterable)
Construct a list from those elements of iterable for which function returns TRUE. Iterable May is either a sequence, a container which supports, or an iteration. If iterable is a string or a tuple, the result also has that type; Otherwise it is always a list. The If function is None, the identity function is assumed, which is, all elements of iterable that are false are removed.
Note This filter (function, iterable) is equivalent to [item as item in Iterable if function (item)] If function are not Non E and [item for item in Iterable if item] if the IF function is None.
The parameter function (which is a function) is used to process each element in the iterable and returns True if the function processes an element, and the element is returned as a member of the list. For example, filter out character A in a string:
Copy Code code as follows:
def func (x):
''' '''
return x!= ' a '
Print filter (func, ' Awake ')
The results of the operation are:
Copy Code code as follows:
This can also be done through list derivation:
Copy Code code as follows:
print '. Join ([x for X in ' Awake ' if x!= ' a '])