Python zip () function usages tutorial _python

Source: Internet
Author: User
Tags python list in python

This article illustrates the definition and usage of the zip () function in Python, which is believed to be useful for Python beginners. Details are as follows:

First, the definition:

Zip ([iterable, ...])
Zip () is a Python built-in function that takes a series of iterative objects as parameters, packs the corresponding elements in the object into tuple (tuples), and returns a list of these tuples. If the length of the incoming argument is unequal, the length of the list is returned and the object with the shortest length in the parameter is the same. Using the * operator, the list can be unzip (uncompressed).

Second, use examples:

Readers take a look at the following example, the basic use of the zip () function is clear:

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

For this is not a very common function, here are a few examples to illustrate its use:

1. Two-D matrix transformation (Matrix-row interchange)

For example, we have a two-dimensional matrix that is described by a list

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

By using the Python list, we can do this task easily.

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 way is to take advantage of 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 also more difficult to understand, the list as tuple decompression, just get our "Row and column interchange" effect, and then by applying the list () function to each element, convert tuple to list

2. Get the element at 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

>>> to I in range:
 Random_pick ("abc", [0.1,0.3,0.6])
 
' C ', '
B '
, ' C ', ' C ' ' C ' ' C '
a ' B ' B ' C '
a
' C '

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

Here we need to explain a little bit, using the Random.uniform () function to generate a random number between 0-1 and copy it to x, using the zip () function to package the element and his corresponding probability into tuple, and then stacking the probabilities of each element until and after the x termination loop
Thus, the probability that the "a" is selected is the probability that the x value is located at 0-0.1, the same "B" as 0.1-0.4, "C" is 0.4-1.0, assuming that X is averaged between 0-1, obviously our purpose has been achieved.

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.