This article transferred from: http://www.jb51.net/article/53051.htm
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:
?
123456789 |
>>> 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
?
1 |
a = [[ 1 , 2 , 3 ], [ 4 , 5 , 6 ], [ 7 , 8 , 9 ]] |
With the Python list derivation method, we can also easily complete this task.
?
12 |
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 approach is to use the ZIP function:
?
12345 |
>>> 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 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
?
123456789101112131415161718192021222324252627 |
>>>
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‘
|
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.
The zip () function in Python