How to create a very large 2 dimensional matrix in python? -


i need 2 dimensional matrix 50,000 rows , 50,000 columns, each element set value 0, while being easy on memory.

i tried this:

value = 5 = [0] * value b = [a] * value b[2][3] = 5 print b 

which gives me

[[0, 0, 0, 5, 0], [0, 0, 0, 5, 0], [0, 0, 0, 5, 0], [0, 0, 0, 5, 0], [0, 0, 0, 5, 0]] 

i know list b reference list 5 times. there way create matrix modifying 1 element not effect others?

if want use numpy there builtin method this:

>>> np.zeros((10,10)) array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],        [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],        [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],        [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],        [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],        [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],        [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],        [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],        [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],        [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]]) 

if not can use list comprehensions, though quite slow.

[[0 r in range(10)] c in range(10)]  

do note there more efficient ways store sparse matrices, such using scipy.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -