码迷,mamicode.com
首页 > 编程语言 > 详细

Python之zip

时间:2015-11-07 12:05:23      阅读:164      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!