2013-04-21 73 views
1

說我是通過遍歷目錄中的Python:二「的」循環的蟒蛇一次

lines = [1, 2, 3, 4] 

linecount = len(lines) 

#I want to be able to do this: 
for i, j in range(linecount - 1, -1, -1), range(linecount, -1, -1): 
    print i, j 

""" 
This would print out 
3 4 
2 3 
1 2 
0 1 
0 0 
""" 

我怎麼會去這樣做呢?

回答

4
for i, j in zip(range(linecount - 1, -1, -1), range(linecount, -1, -1)): 
    print i, j 
+0

謝謝你!而已。 – Tetramputechture 2013-04-21 20:13:54

+3

不過請注意,'zip'會截斷最短的序列,所以您不會得到最終的'0,0'對。參見例如'itertools.izip_longest'以獲得最長序列的方式。 – torek 2013-04-21 20:17:56