两个变量在同一个'for'循环中有两个不同的'range'

时间:2018-06-06 09:21:11

标签: python variables for-loop range

letters=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
for (letters_num,a,z) in range(0,26)  range(1,27) , range(26,0,-1):
    print 'letter' ,letters[letters_num] ,'is number' ,a,'from a and number',z,'from z' 

我希望输出为:

letter a is number 1 from a and number 26 from z
letter b is number 2 from a and number 25 from z
letter c is ......

两个不同'范围'的两个变量放在同一个'for'循环中的正确形式是什么?

for x,y in range('range of x') , range ('range of y'):
print x , y

我想要正确格式,请

2 个答案:

答案 0 :(得分:0)

使用zip

for x, y in zip(range1, range2):
    ....

所以例如

for x, y in zip(range(5), range(6, 11)):
        print(x, y)
0 6
1 7
2 8
3 9
4 10

答案 1 :(得分:0)

答案是使用zip

for letters_num ,a ,z in zip(range(0,26), range(1,27), range(26,0,-1)):
    print 'letter' ,letters[letters_num] ,'is number' ,a,'from a and number',z,'from z'