Hello! 欢迎来到小浪资源网!



Python 中的枚举


请我喝杯咖啡☕

enumerate() 可以创建一个 iterable,它有一个数字加 1 的 iterable,如下所示:
*备注:

  • 第一个参数是可迭代的(必需类型:可迭代)。
  • 第二个参数是 start(optional-default:0-type:int)。
  • iterable 不能直接用索引访问,所以使用 list() 通过索引访问它。
fruits = ["apple", "orange", "banana", "kiwi", "lemon", "mango"]  print(enumerate(iterable=fruits)) print(enumerate(iterable=fruits, start=0)) # <enumerate object at 0x793a2cf15200>  print(list(enumerate(iterable=fruits))) print(list(enumerate(iterable=fruits, start=0))) # [(0, 'apple'), #  (1, 'orange'), #  (2, 'banana'), #  (3, 'kiwi'), #  (4, 'lemon'), #  (5, 'mango')]  print(list(enumerate(iterable=fruits, start=7))) # [(7, 'apple'), #  (8, 'orange'), #  (9, 'banana'), #  (10, 'kiwi'), #  (11, 'lemon'), #  (12, 'mango')]  print(list(enumerate(iterable=fruits))[0]) print(list(enumerate(iterable=fruits, start=0))[0]) # (0, 'apple')  print(list(enumerate(iterable=fruits, start=7))[0]) # (7, 'apple')  print(enumerate(iterable=fruits)[0]) print(enumerate(iterable=fruits, start=0)[0]) print(enumerate(iterable=fruits, start=7)[0]) # error  i, f = list(enumerate(iterable=fruits))[0] i, f = list(enumerate(iterable=fruits, start=0))[0] print(i, f) # 0 apple  i, f = list(enumerate(iterable=fruits, start=7))[0] print(i, f) # 7 apple  for i, f in enumerate(iterable=fruits): for i, f in enumerate(iterable=fruits, start=0):     print(i, f) # 0 apple # 1 orange # 2 banana # 3 kiwi # 4 lemon # 5 mango  for i, f in enumerate(iterable=fruits, start=7):     print(i, f) # 7 apple # 8 orange # 9 banana # 10 kiwi # 11 lemon # 12 mango 
fruits = ["apple", "orange", "banana", "kiwi", "lemon", "mango"]  enum = enumerate  print(list(enum(iterable=enum(iterable=fruits, start=7), start=-3))) # [(-3, (7, 'apple')), #  (-2, (8, 'orange')), #  (-1, (9, 'banana')), #  (0, (10, 'kiwi')), #  (1, (11, 'lemon')), #  (2, (12, 'mango'))]  print(list(enum(iterable=enum(iterable=fruits, start=7), start=-3))[0]) # (-3, (7, 'apple'))  i, jf = list(enum(iterable=enum(iterable=fruits, start=7), start=-3))[0] print(i, jf) # -3 (7, 'apple')  i, (j, f) = list(enum(iterable=enum(iterable=fruits, start=7), start=-3))[0] i, [j, f] = list(enum(iterable=enum(iterable=fruits, start=7), start=-3))[0] print(i, j, f) # -3 7 apple  for i, jf in enum(iterable=enum(iterable=fruits, start=7), start=-3):     print(i, jf) # -3 (7, 'apple') # -2 (8, 'orange') # -1 (9, 'banana') # 0 (10, 'kiwi') # 1 (11, 'lemon') # 2 (12, 'mango')  for i, (j, f) in enum(iterable=enum(iterable=fruits, start=7), start=-3): for i, [j, f] in enum(iterable=enum(iterable=fruits, start=7), start=-3):     print(i, j, f) # -3 7 apple # -2 8 orange # -1 9 banana # 0 10 kiwi # 1 11 lemon # 2 12 mango 
fruits = ["Apple", "Orange", "Banana", "Kiwi", "Lemon", "Mango"]  enum = enumerate  print(list(enum(enum(enum(fruits, start=7), start=-3), start=4))) # [(4, (-3, (7, 'Apple'))), #  (5, (-2, (8, 'Orange'))), #  (6, (-1, (9, 'Banana'))), #  (7, (0, (10, 'Kiwi'))), #  (8, (1, (11, 'Lemon'))), #  (9, (2, (12, 'Mango')))]  print(list(enum(enum(enum(fruits, start=7), start=-3), start=4))[0]) # (4, (-3, (7, 'Apple')))  i, jkf = list(enum(enum(enum(fruits, start=7), start=-3), start=4))[0] print(i, jkf) # 4 (-3, (7, 'Apple'))  i, (jk, f) = list(enum(enum(enum(fruits, start=7), start=-3), start=4))[0] i, [jk, f] = list(enum(enum(enum(fruits, start=7), start=-3), start=4))[0] print(i, jk, f) # 4 -3 (7, 'Apple')  for i, jkf in enum(enum(enum(fruits, start=7), start=-3), start=4):     print(i, jkf) # 4 (-3, (7, 'Apple')) # 5 (-2, (8, 'Orange')) # 6 (-1, (9, 'Banana')) # 7 (0, (10, 'Kiwi')) # 8 (1, (11, 'Lemon')) # 9 (2, (12, 'Mango'))  for i, (j, kf) in enum(enum(enum(fruits, start=7), start=-3), start=4): for i, [j, kf] in enum(enum(enum(fruits, start=7), start=-3), start=4):     print(i, j, kf) # 4 -3 (7, 'Apple') # 5 -2 (8, 'Orange') # 6 -1 (9, 'Banana') # 7 0 (10, 'Kiwi') # 8 1 (11, 'Lemon') # 9 2 (12, 'Mango')  for i, (j, (k, f)) in enum(enum(enum(fruits, start=7), start=-3), start=4): for i, [j, [k, f]] in enum(enum(enum(fruits, start=7), start=-3), start=4):     print(i, j, k, f) # 4 -3 7 Apple # 5 -2 8 Orange # 6 -1 9 Banana # 7 0 10 Kiwi # 8 1 11 Lemon # 9 2 12 Mango 

相关阅读