Brief Introduction to the zip () and * zip () functions in Python and the python Functions

Source: Internet
Author: User

Brief Introduction to the zip () and * zip () functions in Python and the python Functions

Preface

1. experiment environment: Python 3.6;

2. Sample Code address: Download example;

3. The element in this article refers to the next-level project (which may be a single element or nested list) in the list, tuples, dictionaries, and other collection-type data types ).

Detailed description of the zip (* iterables) Function

Zip () function definition

Combine the elements of multiple iterators in the parameters into a new iterator;

Return Value:
Returns a zip object whose internal elements are tuples and can be converted to lists or tuples;

Input parameters: tuples, lists, dictionaries, and other iterators.

Zip () function usage

When the zip () function has only one parameter

Zip (iterable) extracts one tuples from the iterable to form one.

Example:

# Zip () function single parameter list1 = [1, 2, 3, 4] tuple1 = zip (list1) # print the zip function return type print ("zip () return type of the function: \ n ", type (tuple1) # convert a zip object to a list print (" convert a zip object to a list: \ n ", list (tuple1 ))

Output:

The return type of the zip () function:
<Class 'zip'>
Zip object to list:
[(1,), (2,), (3,), (4,)]

When the zip () function has two parameters, the zip (a, B) zip () function extracts one element from a and B to form a tuples, then combine the tuples in turn into a new iterator-the new zip type data.

Note: distinct requires that a and B have the same dimension. When the two have the same number of rows and number of columns, the corresponding position element can be normally combined;

Struct _ longest (* iterables) function.

Example:

m = [[1,2,3], [4,5,6], [7,8,9]] n = [[2,2,2], [3,3,3], [4,4,4]] p = [[2,2,2], [3,3,3,]

Zip (m, n) will return ([1, 2, 3], [2, 2, 2]), ([4, 5, 6], [3, 3, 3]), ([7, 8, 9], [4, 4, 4])

M [0], n [0] M [1], n [1] M [2], n [2]
[1, 2, 3]
[2, 2]
[4, 5, 6]
[3, 3]
[7, 8, 9]
[4, 4]

Zip (m, p) will return ([1, 2, 3], [2, 2, 2]), ([4, 5, 6], [3, 3, 3])

M [0], n [0] M [1], n [1] M [2], n [2]
[1, 2, 3]
[2, 2]
[4, 5, 6]
[3, 3]
[7, 8, 9]

Sample Code:

# The zip () function has two parameters: m = [1, 2, 3], [4, 5, 6], [7, 8, 9] n = [[2, 2, 2], [3, 3, 3], [4, 4, 4] p = [2, 2, 2], [3, 3, 3] # print rows and columns ("rows and columns are the same: \ n", list (zip (m, n ))) # print different rows and columns ("different rows and columns: \ n", list (zip (m, p )))

Output:

The rows and columns are the same:
[([1, 2, 3], [2, 2, 2]), ([4, 5, 6], [3, 3, 3]), ([7, 8, 9], [4, 4, 4])]
Different rows and columns:
[([1, 2, 3], [2, 2, 2]), ([4, 5, 6], [3, 3, 3])]

Zip () function application

Matrix addition and subtraction, dot Multiplication

You can also use the for loop + list derivation;

m = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] n = [[2, 2, 2], [3, 3, 3], [4, 4, 4]]
# Zip () Application # matrix addition and subtraction, point multiplication m = [[1, 2, 3], [4, 5, 6], [7, 8, 9] n = [[2, 2, 2], [3, 3, 3], [4, 4, 4] # print matrix point multiplication ('= *' * 10 + "Matrix point multiplication" + '= *' * 10) print ([x * y for, B in zip (m, n) for x, y in zip (a, B)]) # Add a matrix, subtract the same print ('= *' * 10 + "matrix, subtract "+ '= *' * 10) print ([x + y for a, B in zip (m, n) for x, y in zip (a, B)])

Output:

[2, 4, 6, 12, 15, 18, 28, 32, 36] [3, 4, 5, 7, 8, 9, 11, 12, 13]

* Detailed description of the zip (* iterables) Function

Zip (iterables) Function

* The zip () function is the inverse process of the zip () function. It turns the zip object into the data before the original combination.

Sample Code:

# * Zip () function print ('= *' * 10 + "* zip () function" + '= *' * 10) m = [[1, 2, 3], [4, 5, 6], [7, 8, 9] n = [[2, 2, 2], [3, 3, 3], [4, 4, 4] print ("* zip (m, n) returns: \ n", * zip (m, n) m2, n2 = zip (* zip (m, n) # True is returned if the values are equal. * print (m = list (m2) indicates the inverse process of zip) and n = list (n2 ))

Output:

* Zip (m, n) returns:
([1, 2, 3], [2, 2, 2]) ([4, 5, 6], [3, 3, 3]) ([7, 8, 9], [4, 4, 4])
True

Summary

Complete example: Download example

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.