catalogue
Part II: stream function input and output
(2) reduce iteration accumulation
Part I: zip traversal
In python, the zip() function can compress the elements of multiple iteratable objects together and return an iteratable object. With the help of list and zip() function, the corresponding position elements in each iteratable object are compressed to form a tuple sequence intuitively. The zip() parameter can accept any type of sequence object, and the number of final elements in the tuple depends on the shortest of the iteratable objects.
Without parameters, the zip() function returns an empty tuple sequence. If there is only one iteratable object, the zip() function represents the second iteratable object as an empty sequence.
>>> zip('want','456') <zip object at 0x0000022BA89E0500> >>> list(zip('want','456')) [('w', '4'), ('a', '5'), ('n', '6')]
In addition, note that the zip() object can only be traversed once. After the first traversal, the memory has been released, and the second traversal is empty.
>>> num=zip('abcd','0123') >>> list(num) [('a', '0'), ('b', '1'), ('c', '2'), ('d', '3')] >>> list(num) []
Part II: stream function input and output
In Python, the commonly used functions map(), reduce(), filter(),
(1) map() traversal
Map is a python built-in function with strong functionality. It will map the specified sequence according to the provided function. The built-in function map() can map a function func to each corresponding element of the target object in turn. The map() function does not modify the original sequence or iteration object.
>>> list(map(int,range(6))) [0, 1, 2, 3, 4, 5] >>> def adds(x): return x+2 >>>list(map(adds(5),range(10))) [5,6,7,8,9,10,11,12,13,14]
(2) reduce iteration accumulation
Next, let's explain the reduce() function. Due to the continuous update of Python versions, users generally use Python 3 x. In Python 3 In X, the reduce () function is no longer a built-in function, but by importing the standard library functools. Different from map(), reduce receives functions with two parameters and operates on objects in an iterative cumulative manner. First look at a string of codes:
from functools import reduce >>> reduce(lambda x,y : x+y ,[1,2,3,4,5]) 15
The result is that the five numbers in the list are accumulated, but how does it realize the operation?
lambda is the addition operation of x and y, 1 + 2 = 3 is assigned to the next x, and the value of Y is taken from the list in turn to realize accumulation. The reduce function has three parameters. The first parameter is the action function, the second function is the iteratable object, and the third is the iterative initial value. By importing the standard library operator, you can achieve cumulative multiplication.
(3) filter decision
The built-in function filter() is used to apply a single parameter function to a sequence and return the filter object composed of elements in the sequence whose return value is True. In short, it is equivalent to a filter. The format is filter(function, iterable).
>>> def odds(n): ... if n % 2 != 0: return True ... return False >>> new_list = filter(odds,[1,2,3,4,5]) >>> print(new_list) <filter object at 0x000001F53A481CC0> >>> list(new_list) [1, 3, 5]
These three functions are an important embodiment of Python's support for functional programming. Functional programming decomposes the problem into a series of function operations, and the input flows into and out of a series of functions in turn, so as to finally complete the predetermined tasks and objectives. The idea of functional programming makes the program more modular and improves the efficiency and code reuse rate.
Part III: Enumeration
In C language, enumeration is generally realized through the while() loop, or special enum functions are used. Of course, the same is true in Python. The enumerate() function is used to enumerate the elements in the iteratable object, and the returned iteratable enumerate object, in which each element is a tuple containing index and value. The start parameter is also supported in the enumerate function to specify the start value.
>>> enumerate('apple') <enumerate object at 0x00000201DA1F0540> >>> list(enumerate('apple')) [(0, 'a'), (1, 'p'), (2, 'p'), (3, 'l'), (4, 'e')] >>> list(enumerate('bananas',4)) [(4, 'b'), (5, 'a'), (6, 'n'), (7, 'a'), (8, 'n'), (9, 'a'), (10, 's')]
The enumeration traversal here is the same as zip(). The memory is released after one traversal.
>>> num=enumerate('apple') >>> list(num) [(0, 'a'), (1, 'p'), (2, 'p'), (3, 'l'), (4, 'e')] >>> list(num) []