码迷,mamicode.com
首页 > 其他好文 > 详细

Lists and tuples

时间:2014-07-22 22:53:14      阅读:269      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   2014   for   

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

标签:style   blog   http   io   2014   for   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!