每个人在使用python的过程中都会遍历list和dict. List遍历 最常用最简单的遍历list的方法 a = ["a", "b", "c", "d"] # simple iterate for i in a: print i 但是, 如果我需要拿到list的index, 很多人可能会这样写 a = ["a", "b", "c", "d"]
翻译 给定一个二叉树,返回从下往上遍历经过的每一个节点的值. 从左往右,从叶子到节点. 比如: 给定的二叉树是 {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7 返回它从下往上的遍历结果: [ [15,7], [9,20], [3] ] 原文 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, lev
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example:Given binary tree [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 return its bottom-up level or
Given a matrix of M x N elements (M rows, N columns), return all elements of the matrix in diagonal order as shown in the below image. Example: Input: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] Output: [1,2,4,7,5,3,6,8,9] Explanation: Note: The total
list 遍历index,value list = ['one', 'two', 'three'] for i in list: print(list.index(i),i) #rangefor i in range(len(list)): print(i+1,list[i]) #enumeratefor i, v in enumerate(list): print(i,v) #设置遍历初始位置,只改变起始序号for i, v in enumerate(list, 2): print(i,v)
第一种方法: import collections d = collections.OrderedDict([('a',1),('b',2),('c',3)]) ''' 或者把上面的那一行改成: d = collections.OrderedDict() d['a'] = 1 d['b'] = 2 d['c'] = 3 ''' for k,v in d.items(): print(k,v) 输出结果: a 1 b 2 c 3 第二种方法: from collections import Ord
Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bottom, column by column). If two nodes are in the same row and column, the order should be from left to right. Examples:Given binary tree [3,9,20,null,nu