looping through multiple lists

map: 最大长度输出;
zip: 最短输出;
third: 有序排列;

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

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

# will iterate 2 times,
# the third value of a will not be used
print "Zip:"
for x, y in zip(a, b):
  print "{0}, {1}".format(x, y)

# will iterate 6 times,
# it will iterate over each b, for each a
# producing a slightly different outpu
print "List:"
for x, y in [(x,y) for x in a for y in b]:
    print x, y
================= RESTART: /Users/vivi/Documents/multlist.py =================
Map:
a1 b1
a2 b2
a3 None
Zip:
a1, b1
a2, b2
List:
a1 b1
a1 b2
a2 b1
a2 b2
a3 b1
a3 b2

你可能感兴趣的:(looping through multiple lists)