Can be seen as compression, zip is equivalent to compression in python
Definition of zip() function(Recommended learning:Python video tutorial)
Take elements from multiple iterators in the parameters and combine them into a new iterator;
Return :
Returns a zip object whose internal elements are tuples; it can be converted into a list or a tuple;
Incoming parameters:
Tuple, list , dictionary and other iterators.
Example:
## zip()函数单个参数 list1 = [1, 2, 3, 4] tuple1 = zip(list1) # 打印zip函数的返回类型 print("zip()函数的返回类型:\n", type(tuple1)) # 将zip对象转化为列表 print("zip对象转化为列表:\n", list(tuple1))
Output:
zip()函数的返回类型:zip对象转化为列表: [(1,), (2,), (3,), (4,)]
When the zip() function has two parameters
zip(a,b) The zip() function takes out one element from a and b to form a tuple, and then combines the tuples formed in sequence into a new iterator - new zip type data.
Note:
Requires the dimensions of a and b to be the same. When they have the same number of rows and columns, the corresponding position elements can be combined normally;
When the number of rows or columns of a and b are different, take the smallest number of rows and columns in the two structures, and combine the elements at the corresponding positions according to the smallest number of rows and columns; at this time Equivalent to calling the itertools.zip_longest(*iterables) function.
For more Python related technical articles, please visit thePython Tutorialcolumn to learn!
The above is the detailed content of What does zip mean in python?. For more information, please follow other related articles on the PHP Chinese website!