Python: Resize an existing array and fill with zeros

后端 未结 5 1352
遥遥无期
遥遥无期 2020-12-30 20:55

I think that my issue should be really simple, yet I can not find any help on the Internet whatsoever. I am very new to Python, so it is possible that I am missing somethin

5条回答
  •  有刺的猬
    2020-12-30 21:36

    Another pure python solution is

    a = [1, 2, 3]
    b = []
    for i in range(6):
        b.append((([0] * i) + a[i:i+1] + ([0] * (len(a) - 1 - i)))[:len(a)])
    

    b is now

    [[1, 0, 0], [0, 2, 0], [0, 0, 3], [0, 0, 0], [0, 0, 0], [0, 0, 0]]
    

    it's a hideous solution, I'll admit that. However, it illustrates some functions of the list type that can be used.

提交回复
热议问题