Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Matrix (list) in python

I want to create a matrix ROW x COLS based on another matrix original size ROW x COLS.

ROWS = len(mat)
COLS = len(mat[0])
    
res = [[0 for i in range(ROWS)] for i in range(COLS)]

The above code doesn’t work for the following edge case:

mat = [[3],[4],[5],[5],[3]]

My desired output is:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

However, what I get is:

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

How can I adapt my code to work with any case? ( I do not want to use numpy or any other libraries)

>Solution :

Try:

>>> [[0 for i in range(len(mat[0]))] for j in range(len(mat))]
[[0], [0], [0], [0], [0]]

Also better to use different loop variables instead of i for both:

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading