Example of zip () function usage in Python

Source: Internet
Author: User
Tags python list
This article mainly introduces the example tutorial of zip () function usage in Python, which provides some reference for beginners of Python. if you need it, refer to the example below to describe zip () in Python () the definition and usage of functions are helpful 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.

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.