python的zip函數

1.示例一:

>>> a = [1,2,3]
>>> b = [2,3,4]
>>> ab = zip(a,b)
>>> print ab
[(1, 2), (2, 3), (3, 4)]
>>> type(a)
<type 'list'>
>>> type(ab)
<type 'list'>

2.示例二:

>>> a = [1,2,3]
>>> b = [2,3,4]
>>> c = [3,4,5]
>>> abc = zip(a,b,c)
>>> print abc
[(1, 2, 3), (2, 3, 4), (3, 4, 5)]
>>> type(a)
<type 'list'>
>>> type(abc)
<type 'list'>
>>> abc = zip(*abc)
>>> print abc
[(1, 2, 3), (2, 3, 4), (3, 4, 5)]

3.示例三:

>>> x = (1,2,3)
>>> y = (2,3,4)
>>> xy = zip(x,y)
>>> print xy
[(1, 2), (2, 3), (3, 4)]
>>> type(x)
<type 'tuple'>
>>> type(xy)
<type 'list'>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章