实例(Python 3.0+)
>>>
a
=
[
1
,
2
,
3
]
>>>
b
=
[
4
,
5
,
6
]
>>>
c
=
[
4
,
5
,
6
,
7
,
8
]
>>>
zipped
=
zip
(
a
,
b
)
# 返回一个对象
>>>
zipped
<
zip
object
at
0x103abc288
>
>>>
list
(
zipped
)
# list() 转换为列表
[
(
1
,
4
)
,
(
2
,
5
)
,
(
3
,
6
)
]
>>>
list
(
zip
(
a
,
c
)
)
# 元素个数与最短的列表一致
[
(
1
,
4
)
,
(
2
,
5
)
,
(
3
,
6
)
]
>>>
a1
,
a2
=
zip
(
*
zip
(
a
,
b
)
)
# 与 zip 相反,zip(*) 可理解为解压,返回二维矩阵式
>>>
list
(
a1
)
[
1
,
2
,
3
]
>>>
list
(
a2
)
[
4
,
5
,
6
]
Python 内置函数
['a', 'b', 'c', 'd', 'e', 'f']
[('a', 'b'), ('b', 'c'), ('c', 'd'), ('d', 'e'), ('e', 'f')]