Python language in the enumerate () and zip () function used in the example

In the Python programming language, the enumerate () and zip () are two commonly used built-in functions, similar to the function of these two functions, but slightly different, the following two examples will be described separately.

enumerate () function

This function is literally enumeration, include mean, for a data object to be traversed (e.g., list, string, or tuples) as a combination index sequence, while the data lists and data standard, commonly used in for loop may simultaneously obtain the index values ​​and the corresponding data object. As described for the following example:

Operating results as follows:

Note: In the enumerate () function, the value may be a "start" of the second parameter specifies the initial value of the current index, the following example:

Operating results as follows:

zip () function

This function is used as a parameter iterables plurality of sequentially corresponding element of the object packed into a tuple, and return the object of these tuples.

As described for the following example:

Operating results as follows:

Note: If the number of elements of the various iterables inconsistent, then the object is the same as the shortest length of the returned object may be iterative. For example, for the example above, a reduced list of scores element, modified as follows:

The operating results as follows:

 

Remark

Can be seen from the above two examples, compared to other programming languages, the Python language, the code can be easily implemented in small amounts of more complex functions, herein complete source code for the following test:

 

Guess you like

Origin www.cnblogs.com/valorchang/p/11353161.html