首页 > 其他 > 详细

Lists and tuples

时间:2014-07-19 22:39:21      阅读:404      评论:0      收藏:0      [点我收藏+]

zip is a built-in function that takes two or more sequence and ‘zips’ them into a list of tuples, where each tuple contains one element from each sequence.

 bubuko.com,布布扣                      

The result is a list of tuples where each tuple contains a character from the string and the corresponding element from the list. If the sequences are not the same length, the result gets the length of the shorter one. You can use tuple assignment to traverse a list of tuples:

 bubuko.com,布布扣

If you combine zip, for and tuple assignment, you get a standard idiom for traversing two (or more) sequences at the same time. For example, has_match takes two sequences, t1 and t2, and returns True if there is an index i such that t1[i] == t2[i]:

 bubuko.com,布布扣

If you need to traverse the elements of a sequence and their indices, you can use the built-in function enumerate:

bubuko.com,布布扣 

 

from Thinking in Python

Lists and tuples,布布扣,bubuko.com

Lists and tuples

原文:http://www.cnblogs.com/ryansunyu/p/3855297.html

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