Python | enumerate() 函数

用来添加一个索引


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

比如循环


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

深度学习批量学习

step就是索引


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

文章来源: kings.blog.csdn.net,作者:人工智能博士,版权归原作者所有,如需转载,请联系作者。

原文链接:kings.blog.csdn.net/article/details/109229221

(完)