Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the built-in function zip of python

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report