Example of a zip () function usage in Python tutorial

Source: Internet
Author: User
Tags python list
This paper describes the definition and usage of the zip () function in Python, which is believed to be useful for beginners of python. Details are as follows:

First, the definition:

Zip ([iterable, ...])
Zip () is an intrinsic function of Python that takes a series of iterated objects as parameters, packages the corresponding elements in the object into tuple (tuples), and then returns a list of these tuples. If the length of the passed parameter is not equal, the length of the returned list is the same as the object with the shortest length in the parameter. Using the * operator, you can unzip the list (unzip).

Two, usage example:

Readers 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) [(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-dimensional matrix transformation (matrix of the row and column 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]

With the Python list derivation method, we can also easily complete this task.

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

Another confusing approach 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 (li St,zip (*a)) [[1, 4, 7], [2, 5, 8], [3, 6, 9]]

This method is faster but more difficult to understand, the list as a tuple decompression, just get our "Row and column swap" effect, and then by applying the list () function to each element, convert the tuple to list

2. Acquiring elements 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 & Lt Cumulative_probability:break return item>>> for I in range: Random_pick ("abc", [0.1,0.3,0.6]) ' C ' B ' C ' C ' ' A ' B ' ' C ' ' C ' C ' a ' B ' B ' a ' C ' a ' C '

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

A little explanation here is to use the Random.uniform () function to generate a random number between 0-1 and copy to X, use the zip () function to package the element and his probability into a tuple, and then superimpose the probabilities of each element until and greater than X to terminate the loop
Thus, the probability that "a" is selected is the probability that x is in 0-0.1, the same as "B" is 0.1-0.4, "C" is 0.4-1.0, assuming that x is the average value between 0-1, it is clear that our purpose 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.