How to use the zip function in Python

Introduction

The zip() function is used to take an iterable object as a parameter, pack the corresponding elements in the object into tuples, and then return a list of these tuples.
ps. If the number of elements in each iterator is inconsistent, the length of the returned list is the same as the shortest object. Using the * operator, the tuple can be decompressed into a list.

example

a =[1,2,3]
b =[4,5,6]
c =[4,5,6,7,8]

zipped =zip(a,b) #Packed as a list of tuples
# Output:[(1,4),(2,5),(3,6)]zip(a,c) #The number of elements is consistent with the shortest list
# Output:[(1,4),(2,5),(3,6)]zip(*zipped) #Contrary to zip,*zipped can be understood as decompression and return to a two-dimensional matrix
[(1,2,3),(4,5,6)]

The zip function accepts any number of (including 0 and 1) sequences as parameters and returns a list of tuples.

Look directly at the example:

  1. Example 1:

code show as below:

x =[1,2,3]
y =[4,5,6]
z =[7,8,9]
xyz =zip(x, y, z)
print xyz

The result of running is:

[(1,4,7),(2,5,8),(3,6,9)]

From this result, we can see the basic operation of the zip function.

  1. Example 2:

code show as below:

x =[1,2,3]
y =[4,5,6,7]
xy =zip(x, y)
print xy

The result of running is:

code show as below:

[(1,4),(2,5),(3,6)]

From this result, we can see how the length of the zip function is handled.

  1. Example 3:

code show as below:

x =[1,2,3]
x =zip(x)
print x

The result of running is:

code show as below:

[(1,),(2,),(3,)]

From this result, we can see how the zip function works when there is only one parameter.

  1. Example 4:

code show as below:

x =zip()
print x

The result of running is:

code show as below:

[]

From this result, we can see how the zip function works without parameters.

  1. Example 5:

code show as below:

x =[1,2,3]
y =[4,5,6]
z =[7,8,9]
xyz =zip(x, y, z)
u =zip(*xyz)
print u

The result of running is:

code show as below:

[(1,2,3),(4,5,6),(7,8,9)]

It is generally believed that this is an unzip process, and its operating mechanism is like this:

Before running zip(*xyz), the value of xyz is: [(1, 4, 7), (2, 5, 8), (3, 6, 9)]

Then, zip(*xyz) is equivalent to zip((1, 4, 7), (2, 5, 8), (3, 6, 9))

So, the running result is: [(1, 2, 3), (4, 5, 6), (7, 8, 9)]

Note: The use of *list/tuple in function calls means to separate list/tuple and pass them to the corresponding function as positional parameters (provided that the corresponding function supports an indefinite number of positional parameters)

  1. Example 6:

code show as below:

x =[1,2,3]
r =zip(*[x]*3)
print r

The result of running is:

code show as below:

[(1,1,1),(2,2,2),(3,3,3)]

Its operating mechanism is like this:

[ x] generates a list of lists with only one element x

[ x] * 3 generates a list of lists, it has 3 elements, [x, x, x]

The meaning of zip(* [x] * 3) is clear, zip(x, x, x)

So far, this article on how to use the zip function in Python is introduced. For more related content of the zip function in Python, please search for the previous article of ZaLou.Cn or continue to browse the related articles below. Hope you will support ZaLou more in the future. .Cn!

Recommended Posts

How to use the zip function in Python
How to use the round function in python
How to use the format function in python
How to use SQLite in Python
How to use and and or in Python
How to run id function in python
How to use code running assistant in python
How to install the downloaded module in python
How to use python tuples
How to understand the introduction of packages in Python
How to use Python's filter function
How to use python's help function
How to use hanlp in ubuntu
How to use python thread pool
How Python implements the mail function
How to wrap in python code
How to save the python program
How to omit parentheses in Python
How to write classes in python
How to filter numbers in python
How to read Excel in Python
How to view errors in python
How to write return in python
How Python implements the timer function
How to view the python module
How to understand variables in Python
How to clear variables in python
How to find the area of a circle in python
How to introduce third-party modules in Python
How to represent null values in python
How to use PYTHON to crawl news articles
How to write win programs in python
How to install third-party modules in Python
How to custom catch errors in python
How to write try statement in python
How to define private attributes in Python
How to add custom modules in Python
How to understand global variables in Python
How to view installed modules in python
Python novice learns to use the library
How to learn the Python time module
How to open python in different systems
How to sort a dictionary in python
How to add background music in python
How to represent relative path in python
What is the function of adb in python
How to configure TensorFlow use environment in Ubuntu
Join function in Python
How to program based on interfaces in Python
How to install python in ubuntu server environment
How to simulate gravity in a Python game
An article to understand the yield in Python
How to use dpkg command in Ubuntu system
How to open the ubuntu system in win10
How to enter python through the command line
How to set code auto prompt in python
How to switch the hosts file using python
Teach you how to write games in python
How to delete files and directories in python
Introduction to the use of Hanlp in ubuntu
How to write a confession program in python