热线电话:13121318867

登录
2019-07-03 阅读量: 424
如何在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)]))

0.0000
8
关注作者
收藏
评论(0)

发表评论

暂无数据
推荐帖子