What does zip mean in python?

(*-*)浩
Release: 2019-06-24 11:52:44
Original
23028 people have browsed it

Can be seen as compression, zip is equivalent to compression in python

What does zip mean 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))
Copy after login

Output:

zip()函数的返回类型:  zip对象转化为列表: [(1,), (2,), (3,), (4,)]
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!