1. First look at what is an iterable object
Take the built-in max function as an example to view its doc:Copy codeThe Code is as follows:>>> Print max. _ doc __Max (iterable [, key = func])-> valueMax (a, B, c,... [, key = func])-> value
With a single iterable argument, return its largest item.With two or more arguments, return the largest argument.In the first f
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 arg
the first sacrifice English Documents:
all
( iterable Span class= "Sig-paren")
Return true
If all elements of the iterable was true (or if the iterable is empty). Equivalent to:
def all (iterable): for element in iterable
ABS (x)returns the absolute value of a number. parameters can be normal integers, long integers, or floating-point numbers. If the argument is a complex number, returns its modulus. All (iterable)Returns trueif all elements of the iterable are true (or iterable is empty). any (iterable)Returns trueif any of the elem
The Itertools module, introduced after python2.6, contains functions that create effective iterators, which can be used in various ways to iterate over the data.The iterator returned by all functions in this module can be used in conjunction with a For Loop statement and other functions that contain iterators, such as generators and generator expressions.Note the functions in the Itertools module are created with objects and are iterative objects.1, Itertools.count (start=0, Step=1)Creates an it
', ' value2 ', ' 24],[' 4 ', ' name4 ', ' value4 ', 28],[' 6 ', ' name6 ', ' Value6 ', 32],[' 8 ', ' Name8 ', ' Value8 ', 36],[' Ten ', ' Name10 ', ' Value10 ', 40]]----Calculate the combination of weight values per row:160
Features of map, reduce, and filter functions
Filter Function: Returns a list of elements that meet the criteria, with a list as a parameter, similar to where in SQL A=1map function: Takes a list as a parameter, handles each element, returns a list of these process
', ' Count ', ' extend ', ' index ', ' Insert ', ' pop ', ' remove ', ' reverse ', ' sort ']Divmod () the respective vendor and remainder1 >>> divmod (20,6) 2 (3, 2)Enumerate () Returns an object that can be enumerated, and the next () method of the object returns a tuple1 >>> test = [' A ', ' B ', ' C ']2 >>> for k,v in Enumerate (test): 3 print (K,V) 4 5 # Output: 6 0 A7 1 B8 2 CEval () evaluates the string str as a valid expression and returns the result of the calculation1 >>> s = "1+2
Divided into class and function
1. Class1.1 Class RangeHelp (__builtins__.range)Class Range (object)| Range (stop)-> Range object| Range (start, stop[, step])-> Range Object| Return a sequence of numbers from start to stop by step.We usually use range in the For loop, but the range here is a class rather than a functionThe python3.x range replaces the xrange, from the original built-in function into the class
For NUM in range (4):
print (num, end = ')Output
1.2 class Filter
Class Filter
1. Filter (function,iterable)
Reference
Construct a list from those elements of iterable for which function returns TRUE. Iterable May is either a sequence, a container which supports iteration, or an iterator. If iterable is a string or a tuple, the result also have that type; Otherwise it is always a list. If funct
filter, map, reduce, and zip are built-in methods for sequence data types in Python.
Noun interpretation: The iterable in this article refers to an iterative object, including sequence and iterator, and other container with an iterative nature.
1. Filter (function, iterable)
Filter works by using the first parameter object (function or none) to iterable the se
iterative variable that generates an iterative sequence that results in the same result as the following generator code fragment:1 >>> def f (iterables): 2 for x in Iterables:3 for y in x:4 yield y 5 6 >>> test = f (' ABCDEF ') 7 >>> test.next () 8 ' A ' 9 >>> from itertools import chain12 >>> test = Chain.fro M_iterable (' ABCDEF ') >>> test.next () ' A 'Here's a description:Combinations (iterable, R):Creates an iterator tha
, 10, 10, 10, 10, 10, 10]
Crossing contrast, you know the difference between the two. The original map is the upper and lower operations, reduce is a cross-element operation.
The authoritative explanation comes from the official website:
The code is as follows:
Reduce (function, iterable[, initializer])Apply function of arguments cumulatively to the items of iterable, from left to right, so as to reduce t
Understand Python iterators
What is iteration?
Objects that can directly act on the for loop are collectively referred to as Iterable objects ).The object that can be called by the next () function and continuously return the next value is called an Iterator ).All Iterable can be converted to Iterator through the built-in function iter.
For the iterator, it is enough to have a _ next. When you use the for a
; List1 = [1, 2, 3, 4, 5, 6, 7, 8, 9]>>> List2 = [9, 8, 7, 6, 5, 4, 3, 2, 1]>>> Map (lambda x, y: x + y, list1, list2)[10, 10, 10, 10, 10, 10, 10, 10]
You can see the differences between the two. In the past, map was an upper and lower operation, while reduce was an operation on elements one by one.
Authoritative explanations come from the official website:
Copy codeThe Code is as follows:Reduce (function, iterable [, initializer])Apply function of tw
= [1,2,3,4,5,6,7,8,9]
>>> list2 = [9,8,7,6,5,4,3,2,1]
>>> map (Lambda x,y:x+y, list1,list2)
[10, 10, 10, 10, 10, 10, 10, 10, 10]
Reader contrast, you will know the difference between the two. The original map is up and down, and reduce is the horizontal element of the operation.
The authoritative explanation comes from the official website:
Copy Code code as follows:
Reduce (function, iterable[, initializer])
App
with no content in parentheses, create an empty list parentheses containing data items separated by commas, creating a non-empty list
You can also use List () to create a list:
Returns an empty list when no arguments are specified
Returns a shallow copy of this parameter when using list as a parameter
Attempt to convert a given object to a list type when other parameters are in use
1.3.2 List Index and fragmentation
Grammar
Description
LST[1]
Reading a seco
Brief introduction
Official Description: Functional tools for creating and using iterators. Is the function used to create an efficient iterator.
Itertools.chain (*iterable)
Returns multiple sequences as a single sequence.For example:
Import itertoolsfor itertools.chain (' I ', ' love ', ' Python '): Print each
Output:
Ilovepython
Itertools.combinations (iterable, R)
Returns the "combination" of the spe
? Check the code:
The code is as follows:
>>> List1 = [1, 2, 3, 4, 5, 6, 7, 8, 9]>>> List2 = [9, 8, 7, 6, 5, 4, 3, 2, 1]>>> Map (lambda x, y: x + y, list1, list2)[10, 10, 10, 10, 10, 10, 10, 10]
You can see the differences between the two. In the past, map was an upper and lower operation, while reduce was an operation on elements one by one.
Authoritative explanations come from the official website:
The code is as follows:
Reduce (function, iterable
What is an iteration
An object that can directly act on a for loop is called an iterative object (iterable).An object that can be called by the next () function and continually returns the next value is called an iterator (Iterator).All iterable can be converted to iterator via the built-in function iter ().
For iterators, one __next__ () is enough. When you use the for and in statements, the program automa
, iterable) receives two parameters. The first parameter indicates receiving a function, and the second parameter indicates receiving an iteralbe type object, such as list.
The principle of the map function is: 1. each time a parameter is extracted from iterable, 2. pass this parameter to our function, 3. then add the value returned by the function to a list (this statement is not accurate, just to help you
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.