首頁 >後端開發 >Python教學 >Python 中的列舉

Python 中的列舉

Susan Sarandon
Susan Sarandon原創
2024-12-23 00:27:49721瀏覽

enumerate in Python

請我喝杯咖啡☕

*我的貼文解釋了 zip()。

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

以上是Python 中的列舉的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn