Example of zip () function usage in Python and python example

Source: Internet
Author: User
Tags python list

Example of zip () function usage in Python and python example

This article describes the definition and usage of the zip () function in Python. I believe it is of some reference value for beginners of Python. The details are as follows:

I. Definition:

Zip ([iterable,...])
Zip () is a built-in function of Python. It accepts a series of iteratable objects as parameters, and packs the corresponding elements of objects into tuple (tuples ), then, a list composed of these tuples is returned ). If the length of the input parameter is different, the returned list length is the same as the minimum length of the object in the parameter. Use the * operator to decompress the list unzip file ).

Ii. Usage example:

Take a look at the following example to understand the basic usage of the zip () function:

>>> a = [1,2,3]>>> b = [4,5,6]>>> c = [4,5,6,7,8]>>> zipped = zip(a,b)[(1, 4), (2, 5), (3, 6)]>>> zip(a,c)[(1, 4), (2, 5), (3, 6)]>>> zip(*zipped)[(1, 2, 3), (4, 5, 6)]

This is not a very common function. The following example shows its usage:

1. Two-dimensional matrix transformation (column-and-column interchange of matrices)

For example, we have a two-dimensional matrix described by the list.

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

Through the python list derivation method, we can easily complete this task.

print [ [row[col] for row in a] for col in range(len(a[0]))][[1, 4, 7], [2, 5, 8], [3, 6, 9]]

Another confusing method is to use the zip function:

>>> a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]>>> zip(*a)[(1, 4, 7), (2, 5, 8), (3, 6, 9)]>>> map(list,zip(*a))[[1, 4, 7], [2, 5, 8], [3, 6, 9]]

This method is faster, but it is more difficult to understand. We can regard the list as a tuple to decompress it, so that we can get the effect of "row and column swapping", and then apply the list () function to each element, convert tuple to list

2. Get elements with a specified probability

>>> import random>>> def random_pick(seq,probabilities): x = random.uniform(0, 1) cumulative_probability = 0.0 for item, item_probability in zip(seq, probabilities): cumulative_probability += item_probability if x < cumulative_probability: break return item>>> for i in range(15): random_pick("abc",[0.1,0.3,0.6]) 'c''b''c''c''a''b''c''c''c''a''b''b''c''a''c'

This function has a limit. The list of specified probabilities must correspond to one element and is equal to 1. Otherwise, this function may not work as expected.

Here we need to explain a little bit, first use random. the uniform () function generates a random number between 0 and 1 and copies it to x. The zip () function is used to package the probability of an element and its corresponding probability into a tuple. Then, the probability of each element is superimposed, until and greater than x terminate the loop
In this way, the probability that "a" is selected is the probability that x is at 0-0.1. Similarly, "B" is 0.1-0.4, and "c" is 0.4-1.0, assume that x is an average value between 0 and 1. Obviously, our goal has been achieved.


What is the zip () function in python?

Zip ([1, 2, 3], ['A', 'B', 'C'])
The result is
[(1, 'A'), (2, 'B'), (3, 'C')]

Is to extract the elements of each array in sequence, and then combine
Operands can be more
Zip ([, 3], ['A', 'B', 'C'], [, 6 ])

If the element length is inconsistent, it will be cut to the same length.
In addition, zip (* list) is an asterisk in front of the array, which is the inverse operation of the above operation.
The result of zip (* [(1, 'A'), (2, 'B'), (3, 'C')]) is [1, 2, 3], ['A', 'B', 'C']

Error Message

Related Article

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.