Creating lists of lists in a pythonic way

后端 未结 8 2124
挽巷
挽巷 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:58

    I use

    mat = [[0 for col in range(3)] for row in range(2)]
    

    although depending on what you do with the matrix after you create it, you might take a look at using a NumPy array.

提交回复
热议问题