Python: How to iterate list in reverse order

#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]

时间: 2024-08-03 21:28:10

Python: How to iterate list in reverse order的相关文章

[LeetCode]题解(python):103-Binary Tree Zigzag Level Order Traversal

题目来源: https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/ 题意分析: Z字宽度遍历树. 题目思路: 这题可以用比较取巧的方法.首先获得宽度遍历的结果,然后将第二层的翻转就可以了. 代码(python): # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val

Python基础课:列表方法sort(), reverse()

1 >>> x = list(range(11)) 2 >>> import random 3 >>> random.shuffle(x) #随机乱序 4 >>> x 5 [4, 2, 5, 7, 0, 10, 9, 8, 1, 3, 6] 6 >>> x.sort(key=lambda i:len(str(i)), reverse=True) #指定规则排序 7 >>> x 8 [10, 4, 2

Python MySQL Order By

章节 Python MySQL 入门 Python MySQL 创建数据库 Python MySQL 创建表 Python MySQL 插入表 Python MySQL Select Python MySQL Where Python MySQL Order By Python MySQL Delete Python MySQL 删除表 Python MySQL Update Python MySQL Limit Python MySQL Join 对结果排序 可以使用ORDER BY语句,按升

python 面试题3

注:本面试题来源于网络. 1.python下多线程的限制以及多进程中传递参数的方式 python多线程有个全局解释器锁(global interpreter lock),这个锁的意思是任一时间只能有一个线程使用解释器,跟单cpu跑多个程序一个意思,大家都是轮着用的,这叫“并发”,不是“并行”. 多进程间共享数据,可以使用 multiprocessing.Value 和 multiprocessing.Array 2.Python是如何进行内存管理的? http://developer.51cto

Python基础知识【一】

第一部分: 简易/中等  什么是Python装饰器,如何使用? 你会如何??设置很多项目,其中每一个使用Python的不同版本和第三方库? 什么是PEP8和你是如何遵循它的规范? 参数是如何传递的 - 传值还是传引用? (容易,但又不那么容易,不确定是否能清楚地回答这个问题) 什么是列表解析.字典解析?举个例子 请用三种不同的方法完成"提取列表中每三个项目"? 你知道列表和元组之间的区别么?举个例子? 你知道range和xrange之间的区别?     针对python2.x版本 谈谈

【Python模块学习】4、collections模块

collections是Python内建的一个集合模块,提供了许多有用的集合类. namedtuple() factory function for creating tuple subclasses with named fields deque list-like container with fast appends and pops on either end ChainMap dict-like class for creating a single view of multiple

python公司面试题集锦 python面试题大全

问题一:以下的代码的输出将是什么? 说出你的答案并解释. class Parent(object): x = 1 class Child1(Parent): pass class Child2(Parent): pass print Parent.x, Child1.x, Child2.x Child1.x = 2 print Parent.x, Child1.x, Child2.x Parent.x = 3 print Parent.x, Child1.x, Child2.x 答案 以上代码的

python面试题大全

注:本面试题来源于网络,转载请注明来自http://www.cnblogs.com/goodhacker/p/3366618.html. 1. (1)python下多线程的限制以及多进程中传递参数的方式 python多线程有个全局解释器锁(global interpreter lock),这个锁的意思是任一时间只能有一个线程使用解释器,跟单cpu跑多个程序一个意思,大家都是轮着用的,这叫“并发”,不是“并行”. 多进程间共享数据,可以使用 multiprocessing.Value 和 mult

python爬虫(二)--了解deque

队列-deque有了上面一节的基础,当然你需要完全掌握上一节的所有方法,因为上一节的方法,在下面的教程中会反复的用到.如果你没有记住,请你返回上一节.这一节我们要了解一种队列--deque.在下面的爬虫基础中,我们也要反复的使用deque,来完成网址的出队入队. 有了对deque基本的认识,我们开始进一步的学习了解他. colloections.deque([iterable[,maxlen]])从左到右初始化一个新的deque对象,如果iterable没有给出,那么产生一个空的deque.de