首页 > 编程语言 > 详细

Python之zip

时间:2015-11-07 12:02:59      阅读:205      评论:0      收藏:0      [点我收藏+]
# -*- 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‘)]

 

Python之zip

原文:http://www.cnblogs.com/dengyg200891/p/4944729.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!