这个列表如何遍历
时间: 2023-06-29 13:15:15 浏览: 34
可以使用 for 循环来遍历这个列表,例如:
```python
lst = [3, 5, 7, 9]
for i in lst:
print(i)
```
输出结果为:
```
3
5
7
9
```
也可以使用 while 循环遍历这个列表,例如:
```python
lst = [3, 5, 7, 9]
i = 0
while i < len(lst):
print(lst[i])
i += 1
```
输出结果同样为:
```
3
5
7
9
```
在这个例子中,我们使用了一个计数器 i 和 while 循环来遍历列表。循环条件为 i 小于列表长度 len(lst),每次循环将计数器 i 自增 1,并打印列表中第 i 个元素。
相关问题
python3 列表遍历
以下是三种Python3列表遍历的方法:
1. 使用for循环遍历列表:
```python
list1 = [1, 24, 34, 44, 533, 5, 219]
for item in list1:
print(item)
```
2. 使用索引遍历列表:
```python
list1 = [1, 24, 34, 44, 533, 5, 219]
for i in range(len(list1)):
print(list1[i])
```
3. 使用enumerate()函数遍历列表:
```python
list1 = [1, 24, 34, 44, 533, 5, 219]
for index, item in enumerate(list1):
print(index, item)
```
python 列表遍历
Python的列表遍历可以使用for循环完成,例如:
```
my_list = [1, 2, 3, 4, 5]
for item in my_list:
print(item)
```
这将输出列表中的每个元素:
```
1
2
3
4
5
```
可以在循环中使用条件语句来过滤要处理的元素,例如:
```
my_list = [1, 2, 3, 4, 5]
for item in my_list:
if item % 2 == 0:
print(item)
```
这将输出列表中的偶数:
```
2
4
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)