马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 一个账号 于 2020-3-13 22:34 编辑
Python enumerate() 函数
语法
enumerate(iterable, start=0)
参数
参数 | 描述 | iterable | 可迭代对象 | start | 索引序列的起始值,默认是 0 |
返回值
返回一个迭代器对象。
例子
>>> seasons = ['Spring', 'Summer', 'Autumn', 'Winter']
>>> enumerate(seasons)
<enumerate object at 0x000001E1A8531500>
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Autumn'), (3, 'Winter')]
>>> for i in enumerate(seasons):
print(i)
(0, 'Spring')
(1, 'Summer')
(2, 'Autumn')
(3, 'Winter')
>>> weeks = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']
>>> for index, week in enumerate(weeks, 1):
print(f"星期{index} : {week}")
星期1 : Monday
星期2 : Tuesday
星期3 : Wednesday
星期4 : Thursday
星期5 : Friday
星期6 : Saturday
星期7 : Sunday
>>> list(enumerate(weeks, 1))
[(1, 'Monday'), (2, 'Tuesday'), (3, 'Wednesday'), (4, 'Thursday'), (5, 'Friday'), (6, 'Saturday'), (7, 'Sunday')]
>>> list(enumerate(weeks, 2))
[(2, 'Monday'), (3, 'Tuesday'), (4, 'Wednesday'), (5, 'Thursday'), (6, 'Friday'), (7, 'Saturday'), (8, 'Sunday')]
>>> list(enumerate(weeks, 3))
[(3, 'Monday'), (4, 'Tuesday'), (5, 'Wednesday'), (6, 'Thursday'), (7, 'Friday'), (8, 'Saturday'), (9, 'Sunday')]
>>> list(enumerate(weeks, 4))
[(4, 'Monday'), (5, 'Tuesday'), (6, 'Wednesday'), (7, 'Thursday'), (8, 'Friday'), (9, 'Saturday'), (10, 'Sunday')]
|