热线电话:13121318867

登录
2019-04-19 阅读量: 513
python中的循环扩展

循环扩展:

i)单个循环结构的两个迭代器:在这种情况下,使用枚举函数在单个循环块中的每次迭代中使用列表和字典。让我们看看例子。

# Two separate lists

cars = ["Aston", "Audi", "McLaren"]

accessories = ["GPS kit", "Car repair-tool kit"]

# Single dictionary holds prices of cars and

# its accessories.

# First three items store prices of cars and

# next two items store prices of accessories.

prices = {1:"570000$", 2:"68000$", 3:"450000$",

4:"8900$", 5:"4500$"}

# Printing prices of cars

for index, c in enumerate(cars, start=1):

print "Car: %s Price: %s"%(c, prices[index])

# Printing prices of accessories

for index, a in enumerate(accessories,start=1):

print ("Accessory: %s Price: %s"\

%(a,prices[index+len(cars)]))

输出:

汽车:阿斯顿价格:570000美元
汽车:奥迪价格:68000 $
汽车:麦克拉伦价格:450000 $
附件:GPS套件价格:8900 $
附件:汽车维修工具套件价格:4500 $
0.0000
5
关注作者
收藏
评论(0)

发表评论

暂无数据
推荐帖子