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

looping through multiple lists

时间:2019-11-08 12:19:06      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:loop   lists   nts   multi   The   mes   start   ===   sts   

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

标签:loop   lists   nts   multi   The   mes   start   ===   sts   

原文地址:https://www.cnblogs.com/vivivi/p/11819072.html

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