码迷,mamicode.com
首页 > 编程语言 > 详细

Python: How to iterate list in reverse order

时间:2016-01-04 23:52:23      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:

#1

for index, val in enumerate(reversed(list)):

  print len(list) - index - 1, val

 

#2 

def reverse_enum(L):
  for index in reversed(xrange(len(L))):
    yield index, L[index]

L = [‘foo‘, ‘bar‘, ‘bas‘]
for index, item in reverse_enum(L):
  print index, item

 

#3

L = [‘foo‘, ‘bar‘, ‘bas‘]

for index in reversed(range(len(L))):
  print index, L[index]

Python: How to iterate list in reverse order

标签:

原文地址:http://www.cnblogs.com/lynnge/p/5100458.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!