Creating lists of lists in a pythonic way

后端 未结 8 2132
挽巷
挽巷 2021-02-08 06:18

I\'m using a list of lists to store a matrix in python. I tried to initialise a 2x3 Zero matrix as follows.

mat=[[0]*2]*3

However, when I chang

8条回答
  •  遇见更好的自我
    2021-02-08 06:40

    If the sizes involved are really only 2 and 3,

    mat = [[0, 0], [0, 0], [0, 0]]
    

    is easily best and hasn't been mentioned yet.

提交回复
热议问题