Python | enumerate() 函数

it2026-08-27  6

用来添加一个索引

>>>seasons = ['Spring', 'Summer', 'Fall', 'Winter'] >>> list(enumerate(seasons)) [(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')] >>> list(enumerate(seasons, start=1)) # 下标从 1 开始 [(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]

比如循环

>>>seq = ['one', 'two', 'three'] >>> for i, element in enumerate(seq): ... print i, element ... 0 one 1 two 2 three

深度学习批量学习

step就是索引

for epoch in range(3): for step, (batch_x, batch_y) in enumerate(loader): # 训练 print('Epoch: ', epoch, '| Step: ', step, '| batch x: ', batch_x.numpy(), '| batch y: ', batch_y.numpy())

人工智能博士 认证博客专家 985AI博士 AI专家 博客专家 王博Kings,985AI博士在读,博客专家,华为云专家,是《机器学习手推笔记》、《深度学习手推笔记》等作者,我的微信:Kingsplusa,欢迎交流学习;在人工智能、计算机视觉、无人驾驶等具有丰富的经验。
最新回复(0)