python数组循环
时间: 2024-08-12 16:05:34 浏览: 70
在Python中,数组(列表、元组或数组等)循环通常通过`for`循环实现。以下是三种常见的情况:
1. **遍历列表**:
```python
numbers = [1, 2, 3, 4, 5]
for num in numbers:
print(num)
```
这里,`num`会依次取到列表中的每个元素。
2. **遍历元组**:
元组与列表类似,只是不可变,语法稍有区别:
```python
my_tuple = (6, 7, 8)
for item in my_tuple:
print(item)
```
3. **遍历numpy数组**:
如果你在处理科学计算,可能会用到NumPy库,可以像遍历列表一样操作数组:
```python
import numpy as np
arr = np.array([9, 10, 11])
for value in arr:
print(value)
```
**
阅读全文