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

asked Mar 25, 2019 at 11:35
5
  • 3
    What is mat? Could you provide it? Commented Mar 25, 2019 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 Commented Mar 25, 2019 at 11:39
  • Possible duplicate of How to clone or copy a list? Commented Mar 25, 2019 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 Commented Mar 25, 2019 at 11:51
  • I added a proper answer before seeing your update; have a look and let know if it's not applicable Commented Mar 25, 2019 at 13:23

3 Answers 3

1

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

import copy
fin_mat = copy.deepcopy(mat)
answered Mar 25, 2019 at 11:39
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
answered Mar 25, 2019 at 11:45
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

answered Mar 25, 2019 at 12:02
2
  • I know, I did this . But problem is not solved Commented Mar 26, 2019 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 Commented Mar 26, 2019 at 11:11

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.