Python双层for循环

2020-01-09 15:30:37 浏览数 (1)

代码语言:javascript复制
matrix0 = [[1, 2], [3, 3], [2, 1]]
print 'matrix0:', matrix0

matrix1 = [[1 for i in range(2)] for i in range(3)]
print 'matrix1:', matrix1

matrix2 = [[0 for i in range(2)] for i in range(3)]
print 'matrix2:', matrix2

for i in range(3):
    for j in range(2):
        matrix2[i][j] = matrix0[i][j]   matrix1[i][j]
        
print 'matrix2:', matrix2

print('Good bye')

运行结果:

代码语言:javascript复制
matrix0: [[1, 2], [3, 3], [2, 1]]
matrix1: [[1, 1], [1, 1], [1, 1]]
matrix2: [[0, 0], [0, 0], [0, 0]]
matrix2: [[2, 3], [4, 4], [3, 2]]
Good bye

0 人点赞