Python双层for循环

发布时间:2019-08-28 09:24:24编辑:auto阅读(2337)


    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')
    

    运行结果:

    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
    


关键字