文章来自《Python cookbook》.

翻译仅仅是为了个人学习,其它商业版权纠纷与此无关!

-- Zoom.Quiet [2004-08-11 01:27:58]

1. 通过多个list循环

1.15 Looping Through Multiple Lists

Credit: Andy McKay

1.1. 问题 Problem

You need to loop through every item of multiple lists.

你需要通过多个list的每一个元素来做循环

1.2. 解决 Solution

There are basically three approaches. Say you have:

基本的有三种方法。假定你有:

   1 a = ['a1', 'a2', 'a3']
   2 b = ['b1', 'b2']

Using the built-in function map, with a first argument of None, you can iterate on both lists in parallel:

使用内建函数map,让它的第一个参数是None,你能够在两个list上并行的迭代。

   1 print "Map:"
   2 for x, y in map(None, a, b):
   3     print x, y

The loop runs three times. On the last iteration, y will be None.

循环返回三次。在最后一次的迭代中,y将是None。

Using the built-in function zip also lets you iterate in parallel:

使用内建函数zip也可以让你并行迭代:

   1 print "Zip:"
   2 for x, y in zip(a, b):
   3     print x, y

The loop runs two times; the third iteration simply is not done.

循环返回两次。第三次迭代完全没做。

A list comprehension affords a very different iteration:

一个list内涵提供一个非常不同的迭代

   1 print "List comprehension:"
   2 for x, y in [(x,y) for x in a for y in b]:
   3     print x, y

The loop runs six times, over each item of b for each item of a.

循环运行6次,在b的每个元素上为a的每个元素做循环

1.3. 讨论 Discussion

Using map with None as the first argument is a subtle variation of the standard map call, which typically takes a function as the first argument. As the documentation indicates, if the first argument is None, the identity function is used as the function through which the arguments are mapped. If there are multiple list arguments, map returns a list consisting of tuples that contain the corresponding items from all lists (in other words, it's a kind of transpose operation). The list arguments may be any kind of sequence, and the result is always a list.

使用带None作为第一个参数的map是标准map的一个微妙变化。它典型的是带一个函数作为第一个参数。就像文档指出一样。如果第一个参数是None,同样的函数被使用作为它的参数被映射的功能。如果有多个list参数,map从所有list返回一个由tuple组成的包含相关元素的list(也就是,它是一种转置操作)list参数是任何一种序列,然而结果却肯定是list。

Note that the first technique returns None for sequences in which there are no more elements. Therefore, the output of the first loop is:

注意,第一个技术为在没有更多元素的序列返回None。所以,第一个循环的输出是:

Map: 
a1 b1 
a2 b2 
a3 None 

zip lets you iterate over the lists in a similar way, but only up to the number of elements of the smallest list. Therefore, the output of the second technique is:

zip让你用一个相同的方法在list上迭代。但是只等于最小list的元素的数量。所以,第二个技术输出的结果是:

Zip: 
a1 b1 
a2 b2 

Python 2.0 introduced list comprehensions, with a syntax that some found a bit strange:

Python2.0介绍了list内涵, 它的语法有一些奇怪

   1 [(x,y) for x in a for y in b]

This iterates over list b for every element in a. These elements are put into a tuple (x, y). We then iterate through the resulting list of tuples in the outermost for loop. The output of the third technique, therefore, is quite different:

这个迭代在list b上为每一个a的元素来做。这些元素被放到一个tuple(x,y)中。然后我们通过在最外层的for循环来在tuple的结果list上迭代。第三个技术的输出是完全不同的:

List comprehension: 
a1 b1 
a1 b2 
a2 b1 
a2 b2 
a3 b1 
a3 b2 

1.4. 参考 See Also

The Library Reference section on sequence types; documentation for the zip and map built-ins in the Library Reference.