PythonPython已看

Python_遍历 list

2022-01-17  本文已影响0人  Gakki0725
作者:Gakki

01. 使用 for 遍历列表

test_data = [1, 2, 3, 4, 3, 5, 2, 2]
for value in test_data:
    print(value)
1
2
3
4
3
5
2
2

02. 使用 Python 内置函数 enumerate()

test_data = [1, 2, 3, 4, 3, 5, 2, 2]
for value in enumerate(test_data):
    print(value)
(0, 1)
(1, 2)
(2, 3)
(3, 4)
(4, 3)
(5, 5)
(6, 2)
(7, 2)

03. 使用 iter() 迭代器

test_data = [1, 2, 3, 4, 3, 5, 2, 2]
for value in iter(test_data):
    print(value)
1
2
3
4
3
5
2
2

04. 使用 range() 函数

test_data = [1, 2, 3, 4, 3, 5, 2, 2]
for value in range(len(test_data)):
    print(test_data[value])
1
2
3
4
3
5
2
2
上一篇下一篇

猜你喜欢

热点阅读