0

I have a two-dimensional list with values that I changing. They need to be saved in another two-dimensional list (First list should not be changed).

I tried to set the values directly, but i'm getting this error: IndexError: list index out of range.

That's because nothing is copyied in fin_mat.

How can i put changed values in new list?

for i, i_it in enumerate(mat):
     for j, j_it in enumerate(mat[i]):
       fin_mat[i][j] = mat[i-1][j] + mat[i+1][j] + mat[i][j-1] + mat[i][j+1]

UPD: Okay, I'll try to explain. Program should ask for a string, and convert it in a list that puted in another one to create a two-dimensional list:

b, st = [], [i for i in input().split()]
mat = []

it can be any long and to stop the input you must write: "end"

while (st[0] != 'end'):
  st = [i for i in input().split()]
  b.append(st)
if (st[0] == 'end'):
  del b[-1]

Than you change string values into int

for j in b:
  r = [int(item) for item in j]
  mat.append(r)
print(mat)

After that, I need to created another matrix in which elements must be defined by this formula:
(i-1, j) + (i+1, j) + (i, j-1) + (i, j+1) = fin_mat[ i ][ j ]

I can't just copy first list, and I can't change it because the values from the first list is in this formula.

I need to add values one by one in fin_mat

  • 3
    What is `mat`? Could you provide it? – YusufUMS Mar 25 '19 at 11:37
  • 1
    The problem looks like is with the list indices on each iteration; in short when 1 = 0 you try to access `mat[-1][j]` and this obviously will happen when you get to j+1 – nickyfot Mar 25 '19 at 11:39
  • Possible duplicate of [How to clone or copy a list?](https://stackoverflow.com/questions/2612802/how-to-clone-or-copy-a-list) – soheshdoshi Mar 25 '19 at 11:46
  • You should explain the logic behing the changing you want to do. There is also an `m` in `mat[i-m+1]` which is not declared anywhere (not in the code you provided at least). Please provide a [MCVE](https://stackoverflow.com/help/mcve) – Valentino Mar 25 '19 at 11:51
  • I added a proper answer before seeing your update; have a look and let know if it's not applicable – nickyfot Mar 25 '19 at 13:23

3 Answers3

1

Create a copy of the original matrix first, like this:

import copy
fin_mat = copy.deepcopy(mat)
ErikXIII
  • 557
  • 2
  • 12
1

I think is easier to do it without enumerate like this example:

import cv2
img = cv2.imread("./Screenshot.png",1)
print(img.shape) # output is (961, 1744, 3)
for i in range(len(img)):
    for j in range(len(img[i])):
        img[i][j]=5
Zaher88abd
  • 448
  • 7
  • 20
0

There are a few questions around the question itself, but I think I understand what you problem and question is.

Essentially, you do not only want to copy the list but also do some transformations without changing the original.

As previously pointed out the copying part is already answered in other questions. Regarding your IndexError, this is due to the logic of the loops; basically when i=0 you try to access mat[-1][j]

To correct this, you need to add some logic around what happens when you are on the edge of your matrix (in this example it's 0, but feel free to change):

fin_mat = fin_mat = [[0]*len(mat[0])]*len(mat) #creates an empty list of the same dimensions 
for i, i_it in enumerate(mat):
    for j, j_it in enumerate(mat[i]):
        left = mat[i-1][j] if i > 0 else 0
        right = mat[i+1][j] if i < len(mat)-1 else 0
        top = mat[i][j-1] if j > 0 else 0
        bottom = mat[i][j+1] if j < len(mat[i])-1 else 0
        fin_mat[i][j] = left + right + top + bottom

Hope this helps

nickyfot
  • 1,932
  • 17
  • 25
  • I know, I did [this](https://repl.it/repls/HospitableLoudElectricity) . But problem is not solved – Misha Salavatov Mar 26 '19 at 10:59
  • i don't quite get the logic, I think the nested if-else in this case do not help with readability, especially if you add it in the for loop but it seems to [run](https://repl.it/repls/EvergreenBarrenRectangle) – nickyfot Mar 26 '19 at 11:11