The Seven Year Itch.

MENU

python for循环如何遍历多个数组 和 遍历list(列表)

March 17, 2017 • Read: 647 • Python

i = [1,2,3]

o = [4,5,6]

for i2,o2 in zip(i,o):
    print i2,o2

结果:

1 4
2 5
3 6

字典:

i = {"1":"123","2":"abc"}

o = {"3":"456","4":"def"}

for k,v in zip(i,o):
    print i[k],o[v]

结果:

123 456
abc def

=======遍历List=======

list = ['html', 'js', 'css', 'python']

# 方法1
print '遍历列表方法1:'
for i in list:
    print ("序号:%s   值:%s" % (list.index(i) + 1, i))

print '\n遍历列表方法2:'
# 方法2
for i in range(len(list)):
    print ("序号:%s   值:%s" % (i + 1, list[i]))

# 方法3
print '\n遍历列表方法3:'
for i, val in enumerate(list):
        print ("序号:%s   值:%s" % (i + 1, val))

    # 方法3
    print '\n遍历列表方法3 (设置遍历开始初始位置,只改变了起始序号):'
Tags: None
Archives QR Code
QR Code for this page
Tipping QR Code