Python之zip

# -*- coding: utf-8 -*-
#python 27
#xiaodeng
#Python之zip
#http://python.jobbole.com/82590/



#1)zip语法格式:
'''
zip(...)
    zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]
    
    Return a list of tuples, where each tuple contains the i-th element
    from each of the argument sequences.  The returned list is truncated
    in length to the length of the shortest argument sequence.
'''
#seq1,seq2:序列




#案例
#每次循环时,从各个序列分别从左到右取出一个元素,合并成一个tuple,然后再组装成list。
list1=[1,2,3]
list2=['xiaodeng','xiaochen','xiaoni']
print zip(list1,list2)#[(1, 'xiaodeng'), (2, 'xiaochen'), (3, 'xiaoni')]


#支持2个以上list组装。
ta = [1,2,3]
tb = [9,8,7]
tc = ['a','b','c']
print zip(ta,tb,tc)#[(1, 9, 'a'), (2, 8, 'b'), (3, 7, 'c')]


#list长度不对等情况下,按照一般人思维呈现。
list1=[1,2,3,4]
list2=['xiaodeng','xiaochen','xiaoni']
print zip(list1,list2)#[(1, 'xiaodeng'), (2, 'xiaochen'), (3, 'xiaoni')]
list1=[1,2,3]
list2=['xiaodeng','xiaochen']
print zip(list1,list2)#[(1, 'xiaodeng'), (2, 'xiaochen')]

 

posted @ 2015-11-07 11:12  Xiao|Deng  阅读(186)  评论(0编辑  收藏  举报