감동, 마음이 움직이는 것

[python] 정해진 사이즈의 2d list 만들기 본문

Tips (Utility, Computer Language, and etc.)

[python] 정해진 사이즈의 2d list 만들기

Struggler J. 2019. 1. 23. 06:10

https://stackoverflow.com/questions/2397141/how-to-initialize-a-two-dimensional-array-in-python

How to initialize a two-dimensional array in Python?

x = [[0 for i in range(Nc)] for j in range(Nr)] #making Nr*Nc matrix (2d array)

In [1]: Nc = 5; Nr = 3


In [2]: x = [[0 for i in range(Nc)] for j in range(Nr)]


In [3]: x


Out[3]: [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]


In [4]: x[1]


Out[4]: [0, 0, 0, 0, 0]