Python 数组遍历:for循环和while循环详解
在Python中,可以使用for循环和while循环来遍历数组中的信息。
使用for循环遍历数组
例如,假设我们有一个名为'arr'的数组,可以使用以下代码遍历它:
arr = [1, 2, 3, 4, 5]
for i in arr:
print(i)
这将打印数组中的每个元素:
1
2
3
4
5
使用while循环和索引遍历数组
您还可以使用while循环和索引来遍历数组。例如:
arr = [1, 2, 3, 4, 5]
i = 0
while i < len(arr):
print(arr[i])
i += 1
这将输出相同的数组元素:
1
2
3
4
5
原文地址: https://www.cveoy.top/t/topic/nDTG 著作权归作者所有。请勿转载和采集!