p y t hon中enumerate
时间: 2023-04-20 12:02:11 浏览: 74
当我们在 Python 中需要遍历一个列表或者序列时,我们可以使用 `enumerate()` 函数。这个函数会返回一个元组序列,其中每个元组包含两个元素,第一个是当前元素的索引,第二个是当前元素的值。以下是一个使用 `enumerate()` 函数的例子:
```python
fruits = ['apple', 'banana', 'cherry']
for index, fruit in enumerate(fruits):
print(index, fruit)
```
输出:
```
0 apple
1 banana
2 cherry
```
在这个例子中,我们使用 `enumerate()` 函数来遍历 `fruits` 列表,并在每次循环中打印出当前元素的索引和值。
相关问题
p y t hon enumerate
enumerate() 是 Python 中的内置函数,它可以将一个可迭代对象(如列表、字符串等)的元素和它的索引组成一个新的可迭代对象。语法格式如下:
enumerate(iterable, start=0)
参数说明:
- iterable:要迭代的对象,如列表、字符串等。
- start:索引的起始值,默认为0。
使用示例:
```
>>> for index, value in enumerate(['a', 'b', 'c']):
... print(index, value)
...
0 a
1 b
2 c
```
上面的示例输出了列表 ['a', 'b', 'c'] 中每个元素的索引和值。
p y t hon中strftime
strftime是Python中的一个函数,用于将日期和时间格式化为字符串。它可以将日期和时间按照指定的格式输出,例如将日期输出为"2022-01-01",将时间输出为"12:30:00"等等。使用strftime函数需要传入两个参数,第一个参数是格式化字符串,第二个参数是一个时间元组。
相关推荐
![](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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)