C型方法:这种方法需要迭代总数的先验知识。
# A C-style way of accessing list elements
cars = ["Aston", "Audi", "McLaren"]
i = 0
while (i < len(cars)):
print(cars[i])
i += 1
输出
Aston
Audi
McLaren
注意事项:
for-in(或for each)样式的方法:这种风格在Python中使用,包含列表迭代器,字典,n维数组等。迭代器获取每个组件并在循环时打印数据。迭代器在这个构造中自动递增/递减。
# Accessing items using for-in loop
cars = ["Aston", "Audi", "McLaren"]
for x in cars:
print(x)
输出
Aston
Audi
McLaren
使用range函数进行索引:我们也可以在Python中使用range()来进行索引。
# Accessing items using indexes and for-in
cars = ["Aston", "Audi", "McLaren"]
for i in range(len(cars)):
print(cars[i])
输出
Aston
Audi
McLaren
enumerate是一个内置的Python函数,它将输入作为迭代器、列表等,并返回一个包含索引和迭代器序列中该索引处的数据的元组。例如,enumerate(cars)返回一个迭代器,该迭代器将返回(0,cars[0])、(1,cars[1])、(2,cars[2]),依此类推。
# Accessing items using enumerate()
cars = ["Aston", "Audi", "McLaren "]
for i, x in enumerate(cars):
print(x)
输出
Aston
Audi
McLaren
下面的解决方案也可以。
# Accessing items and indexes enumerate()
cars = ["Aston" , "Audi", "McLaren "]
for x in enumerate(cars):
print (x[0], x[1])
输出
(0, 'Aston')
(1, 'Audi')
(2, 'McLaren ')
我们也可以直接打印enumerate()的返回值,看看它返回了什么。
# Printing return value of enumerate()
cars = ["Aston" , "Audi", "McLaren "]
print (enumerate(cars))
输出
<enumerate object at 0x7fe4f914d3c0>
enumerate采用参数start,默认设置为零。我们可以将此参数更改为任何我们喜欢的值。在下面的代码中,我们使用start 为1。
# demonstrating the use of start in enumerate
cars = ["Aston" , "Audi", "McLaren "]
for x in enumerate(cars, start=1):
print (x[0], x[1])
输出
(1, 'Aston')
(2, 'Audi')
(3, 'McLaren ')
enumerate()有助于嵌入用于访问迭代器中的每个数据项的解决方案,并获取每个数据项的索引。
i)单个循环构造的两个迭代器:在这种情况下,列表和字典将用于使用enumerate函数的单个循环块中的每次迭代。让我们看一个例子。
# 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)]))
输出
Car: Aston Price: 570000$
Car: Audi Price: 68000$
Car: McLaren Price: 450000$
Accessory: GPS kit Price: 8900$
Accessory: Car repair-tool kit Price: 4500$
ii)zip函数(两个迭代器都用于单个循环构造):此函数有助于在第i个位置类似类型的迭代器(列表-列表或dict- dict等)数据项。它使用这些输入迭代器的最短长度。其他更长的迭代器项将被跳过。如果迭代器为空,则返回No output。
例如,对两个列表(迭代器)使用zip。
# Python program to demonstrate the working of zip
# Two separate lists
cars = ["Aston", "Audi", "McLaren"]
accessories = ["GPS", "Car Repair Kit",
"Dolby sound kit"]
# Combining lists and printing
for c, a in zip(cars, accessories):
print("Car: %s, Accessory required: %s" % (c, a))
输出
Car: Aston, Accessory required: GPS
Car: Audi, Accessory required: Car Repair Kit
Car: McLaren, Accessory required: Dolby sound kit
zip函数中的这些迭代器的反向操作称为使用“*”运算符解包。enumerate函数和zip函数的使用有助于实现python中迭代逻辑的有效扩展,解决了一个巨大任务或问题的更多子问题。
# Python program to demonstrate unzip (reverse
# of zip)using * with zip function
# Unzip lists
l1,l2 = zip(*[('Aston', 'GPS'),
('Audi', 'Car Repair'),
('McLaren', 'Dolby sound kit')
])
# Printing unzipped lists
print(l1)
print(l2)
输出
('Aston', 'Audi', 'McLaren')
('GPS', 'Car Repair', 'Dolby sound kit')