Logo

Programming-Idioms

History of Idiom 26 > diff from v94 to v95

Edit summary for version 95 by programming-idioms.org:
Restored version 90: No.

Version 94

2021-12-19, 13:28:29

Version 95

2021-12-20, 11:11:31

Idiom #26 Create a 2-dimensional array

Declare and initialize a matrix x having m rows and n columns, containing real numbers.

Idiom #26 Create a 2-dimensional array

Declare and initialize a matrix x having m rows and n columns, containing real numbers.

Variables
x,m,n
Variables
x,m,n
Extra Keywords
2d
Extra Keywords
2d
Code
print(5)
Code
print(x, m, n)
Code
x = [[0] * n for _ in range(m)]
Comments bubble
Do not just "multiply by m", which would duplicate the references to the inner arrays.
Comments bubble
Do not just "multiply by m", which would duplicate the references to the inner arrays.
Code
print(x, m ,n )
Code
x = [[0 for j in xrange(n)] for i in xrange(m)]
Comments bubble
Python 2.7

xrange doesn't exist in Python 3.
Comments bubble
Python 2.7

xrange doesn't exist in Python 3.
Origin
http://stackoverflow.com/a/6667288/871134
Origin
http://stackoverflow.com/a/6667288/871134