Python - how to create a two-dimensional matrix with for loops

Asked

Viewed 884 times

1

Hello The code below gives the following error message:

Traceback (Most recent call last): File "C:/Users/Usuario/Documents/tests python/várias.py", line 12, in mult[i][j] = (i*j) Indexerror: list assignment index out of range

I ask you, who is out of range? i? j? What to analyze (I changed all ranges with all possible values) when you receive this message (I know it means that you are trying to access an element outside the established limits), because I don’t see what or who is outside. Objetivo:[[0,0,0],[0,1,2],[0,2,4]...[0,10,20]] Thank you very much.

mult =[[] for _ in range(10)]

for i in range(10):
    for j in range(3):
        mult[i][j] = (i*j)

print(mult)
  • i who is out of the range. You have feathers a list containing 10 other empty lists. Try this way print([[i*j for j in range(3)] for i in range(10)])

2 answers

2


You are creating a list containing 10 elements. Each element in this list is an empty list, see only:

x = [[] for _ in range(10)]
print(x)

Exit:

[[], [], [], [], [], [], [], [], [], []]

If you really want an empty two-dimensional array, I suggest something like:

x = [[0 for _ in range(3)] for _ in range(10)]
print(x)

Exit:

[[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]]

Look what your code would look like:

mult = [[0 for _ in range(3)] for _ in range(10)]

for i in range(10):
    for j in range(3):
        mult[i][j] = (i*j)

print(mult)

Exit:

[[0, 0, 0], [0, 1, 2], [0, 2, 4], [0, 3, 6], [0, 4, 8], [0, 5, 10], [0, 6, 12], [0, 7, 14], [0, 8, 16], [0, 9, 18]]

To make your life easier, you could implement a function capable of generating two-dimensional matrices:

def createArray2D( xdim, ydim, default=0 ):
    return [[default for _ in range(xdim)] for _ in range(ydim)]

mult = createArray2D( 3, 10, 0 )

for i in range(10):
    for j in range(3):
        mult[i][j] = (i*j)

print(mult)

Or use the standard library numpy to do the job:

import numpy

mult = numpy.zeros((10, 3))

for i in range(10):
    for j in range(3):
        mult[i][j] = (i*j)

print(mult)

Exit:

[[ 0.  0.  0.]
 [ 0.  1.  2.]
 [ 0.  2.  4.]
 [ 0.  3.  6.]
 [ 0.  4.  8.]
 [ 0.  5. 10.]
 [ 0.  6. 12.]
 [ 0.  7. 14.]
 [ 0.  8. 16.]
 [ 0.  9. 18.]]

1

This syntax of assignment mult[i][j] = (i*j) will not work on your list because the list internal is empty. Try using the append method as in the example below

mult =[[] for _ in range(10)]

for i in range(10):
    for j in range(3):
        mult[i].append(i*j)

print(mult)

The example above was to use the code of your example. But you don’t even need to set the positions with mult =[[] for _ in range(10)]. With the use of append it is possible to achieve the same result.

mult =[]
for i in range(10):
    mult.append([])
    for j in range(3):
        mult[i].append(i*j)

print(mult)

Finally you can do list coverage and put all this in a row as follows:

mult =[[i*j for j in range(3)] for i in range(10)]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.