无法在python中制作10x10矩阵

问题描述 投票:0回答:1

我想写一个简单的脚本来创建矩阵10x10,所有数字从1到99每次附加到一个由10个元素组成的列表组。我期望的结果是list [[1,2,3,4,5,6,7,8,9],[10,11,12,13,14,15,16,17,18,19]。 ..]但是输出很奇怪:

这里是脚本:

lista=[]
lista2=[]
z=0
for a in range (10):
    lista2.clear()
    for x in range (10):
        lista2.append(z)
        z+=1
    print(lista2)
    lista.append(lista2)
    print(lista)

这里是输出:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
[[10, 11, 12, 13, 14, 15, 16, 17, 18, 19], [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]]
[20, 21, 22, 23, 24, 25, 26, 27, 28, 29]
[[20, 21, 22, 23, 24, 25, 26, 27, 28, 29], [20, 21, 22, 23, 24, 25, 26, 27, 28, 29], [20, 21, 22, 23, 24, 25, 26, 27, 28, 29]]
[30, 31, 32, 33, 34, 35, 36, 37, 38, 39]
[[30, 31, 32, 33, 34, 35, 36, 37, 38, 39], [30, 31, 32, 33, 34, 35, 36, 37, 38, 39], [30, 31, 32, 33, 34, 35, 36, 37, 38, 39], [30, 31, 32, 33, 34, 35, 36, 37, 38, 39]]
[40, 41, 42, 43, 44, 45, 46, 47, 48, 49]
[[40, 41, 42, 43, 44, 45, 46, 47, 48, 49], [40, 41, 42, 43, 44, 45, 46, 47, 48, 49], [40, 41, 42, 43, 44, 45, 46, 47, 48, 49], [40, 41, 42, 43, 44, 45, 46, 47, 48, 49], [40, 41, 42, 43, 44, 45, 46, 47, 48, 49]]
[50, 51, 52, 53, 54, 55, 56, 57, 58, 59]
[[50, 51, 52, 53, 54, 55, 56, 57, 58, 59], [50, 51, 52, 53, 54, 55, 56, 57, 58, 59], [50, 51, 52, 53, 54, 55, 56, 57, 58, 59], [50, 51, 52, 53, 54, 55, 56, 57, 58, 59], [50, 51, 52, 53, 54, 55, 56, 57, 58, 59], [50, 51, 52, 53, 54, 55, 56, 57, 58, 59]]
[60, 61, 62, 63, 64, 65, 66, 67, 68, 69]
[[60, 61, 62, 63, 64, 65, 66, 67, 68, 69], [60, 61, 62, 63, 64, 65, 66, 67, 68, 69], [60, 61, 62, 63, 64, 65, 66, 67, 68, 69], [60, 61, 62, 63, 64, 65, 66, 67, 68, 69], [60, 61, 62, 63, 64, 65, 66, 67, 68, 69], [60, 61, 62, 63, 64, 65, 66, 67, 68, 69], [60, 61, 62, 63, 64, 65, 66, 67, 68, 69]]
[70, 71, 72, 73, 74, 75, 76, 77, 78, 79]
[[70, 71, 72, 73, 74, 75, 76, 77, 78, 79], [70, 71, 72, 73, 74, 75, 76, 77, 78, 79], [70, 71, 72, 73, 74, 75, 76, 77, 78, 79], [70, 71, 72, 73, 74, 75, 76, 77, 78, 79], [70, 71, 72, 73, 74, 75, 76, 77, 78, 79], [70, 71, 72, 73, 74, 75, 76, 77, 78, 79], [70, 71, 72, 73, 74, 75, 76, 77, 78, 79], [70, 71, 72, 73, 74, 75, 76, 77, 78, 79]]
[80, 81, 82, 83, 84, 85, 86, 87, 88, 89]
[[80, 81, 82, 83, 84, 85, 86, 87, 88, 89], [80, 81, 82, 83, 84, 85, 86, 87, 88, 89], [80, 81, 82, 83, 84, 85, 86, 87, 88, 89], [80, 81, 82, 83, 84, 85, 86, 87, 88, 89], [80, 81, 82, 83, 84, 85, 86, 87, 88, 89], [80, 81, 82, 83, 84, 85, 86, 87, 88, 89], [80, 81, 82, 83, 84, 85, 86, 87, 88, 89], [80, 81, 82, 83, 84, 85, 86, 87, 88, 89], [80, 81, 82, 83, 84, 85, 86, 87, 88, 89]]
[90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
[[90, 91, 92, 93, 94, 95, 96, 97, 98, 99], [90, 91, 92, 93, 94, 95, 96, 97, 98, 99], [90, 91, 92, 93, 94, 95, 96, 97, 98, 99], [90, 91, 92, 93, 94, 95, 96, 97, 98, 99], [90, 91, 92, 93, 94, 95, 96, 97, 98, 99], [90, 91, 92, 93, 94, 95, 96, 97, 98, 99], [90, 91, 92, 93, 94, 95, 96, 97, 98, 99], [90, 91, 92, 93, 94, 95, 96, 97, 98, 99], [90, 91, 92, 93, 94, 95, 96, 97, 98, 99], [90, 91, 92, 93, 94, 95, 96, 97, 98, 99]]


**added print(lista2) to chechk that there were only 10 elements each time in it`enter code here`
python list for-loop append output
1个回答
0
投票
lista = []
z = 0

for i in range(10):
    lista2 = []
    for j in range(10):
        lista2.append(z)
        z += 1
    lista.append(lista2)

print(lista)
© www.soinside.com 2019 - 2024. All rights reserved.