Python中循环数组有几种方式:
arr = [1, 2, 3, 4, 5]
for element in arr:
print(element)
enumerate() 函数,在循环中同时获取索引和元素
arr = [1, 2, 3, 4, 5]
for index, element in enumerate(arr):
print(index, element)
arr = [1, 2, 3, 4, 5]
i = 0
while i < len(arr):
print(arr[i])
i += 1
arr = [1, 2, 3, 4, 5]
new_list = [x*2 for x in arr]
print(new_list)
def print_array(arr):
for element in arr:
if isinstance(element, list):
print_array(element)
else:
print(element)
arr = [1, 2, [3, 4, [5, 6]], 7]
print_array(arr)