Get the App
SLTechnology News&Howtos  ›  Development  › 

How to understand the built-in function zip of python

Shulou Source: shulou.com Published: 2022-06-02 01:52:27 10月06日 Update

What this article shares with you is about how to understand the built-in function zip of python. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

What does python mean Python is a cross-platform, interpretive, compiled, interactive and object-oriented scripting language, originally designed to write automated scripts, and is often used to develop independent and large-scale projects as versions are constantly updated and new features are added.

I. brief introduction

The zip () function is used to take iterable objects as parameters, and its main function is to package the corresponding elements in the object into tuples, and then return a list of these tuples.

If the number of elements of each iterable iterator is different, the length of the return list is the same as that of the shortest object, and the tuple can be decompressed into a list using the * operator.

Key point: pack it into tuples and return a list. If the length is inconsistent, align with the short iterable.

Second, detailed explanation

Syntax: zip ([iterable,...])

Parameter: iterable is one or more objects that can be iterated

III. Code

Example 1: the elements of the two lists are represented by x coordinates and y coordinates, respectively, and packed into coordinate pairs of points.

X = [1,2,3] y = [4,5,6] for i in zip (x, y): print (I) (1,4) (2,5) (3,6)

Example 2: find the inner product of the same vector of two dimensions

Def innerproduct (vector1, vector2): return sum ([I * j for i, j in zip (vector1, vector2)]) print (innerproduct ([1Jing 2Jing 3], [1Med 2Jing 3])) above is how to understand zip, the built-in function of python. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Tags: Objects functions elements coordinates iterations same consistent two parameters more knowledge articles scripts length projects practical continuous number interaction code Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Docker Shulou Information Huawei Shulou Technology