Change the value of a number in a matrix without knowing the position (index) in Python

Asked

Viewed 2,568 times

2

Hello, How do I change the value of a number in a matrix when I don’t know what its index is and there might be more than one value to change? For example, if I have the matrix:

[[4,5,3,15,4],
 [20,17,3,4,56],
 [5,6,2,90,32],
 [18,7,1,8,13],
 [0,20,30,4,7]]

And I want all the values that are smaller or equal 4 to be changed to 10, how do I do? Specifically a repeating structure, as the matrix may have different size depending on the case. I tried to do it this way:

lista  = [[4,5,3,15,4],
          [20,17,3,4,56],
          [5,6,2,90,32],
          [18,7,1,8,13],
          [0,20,30,4,7]]
numero <= 4
if numero in lista:
    lista[lista.index(4)] = 10    
print (lista)

However, the repeating structure does not work... The output should be:

 [[10,5,10,15,10],
  [20,17,10,10,56],
  [5,6,10,90,32],
  [18,7,10,8,13],
  [10,20,30,10,7]]

2 answers

2

The.index() list will return only the smallest index at which this number occurs, use a for loop to change the values.

lista  = [[4,5,3,15,4],
          [20,17,3,4,56],
          [5,6,2,90,32],
          [18,7,1,8,13],
          [0,20,30,4,7]]

for i in range(0,len(lista)):
    for j in range(0,len(lista[i])):
        if lista[i][j] <= 4:
            lista[i][j] = 10

print(lista)

1

  1. The line numero <= 4 doesn’t make much sense, it’s doing absolutely nothing.
  2. Remember that you have a list of lists, so the logic inside the for is is not correct, you must access two indices, the sublist and the number inside that southern to get compare/change its value.
  3. lista.index(...) only returns the index of the first occurrence of a given element (4, in this case) within a list

Using cycle for, enumerate and doing it the way pythonica:

lista = [[4,5,3,15,4],
         [20,17,3,4,56],
         [5,6,2,90,32],
         [18,7,1,8,13],
         [0,20,30,4,7]]

for idx1, i in enumerate(lista): # percorrer elementos (sublistas), e respetivos indices, contidos na lista principal
  for idx2, j in enumerate(lista[idx1]): # percorrer elementos e respetivos indices contidos na sublista, aqui enumerate(i) tambem daria
    if(j <= 4):
      lista[idx1][idx2] = 10 # alterar valor

print(lista) # [[10, 5, 10, 15, 10], [20, 17, 10, 10, 56], [5, 6, 10, 90, 32], [18, 7, 10, 8, 13], [10, 20, 30, 10, 7]]

DEMONSTRATION

That using list compreension may be reduced to one line:

lista = [[4,5,3,15,4],
         [20,17,3,4,56],
         [5,6,2,90,32],
         [18,7,1,8,13],
         [0,20,30,4,7]]

lista = [[10 if j <= 4 else j for j in i] for i in lista]
# [[10, 5, 10, 15, 10], [20, 17, 10, 10, 56], [5, 6, 10, 90, 32], [18, 7, 10, 8, 13], [10, 20, 30, 10, 7]]

DEMONSTRATION

With numpy and place:

import numpy as np

lista = [[4,5,3,15,4],
         [20,17,3,4,56],
         [5,6,2,90,32],
         [18,7,1,8,13],
         [0,20,30,4,7]]

arr = np.asarray(lista)
np.place(arr, arr<=4, [10])

print(arr) # [[10 5 10 15 10] [20 17 10 10 56] [ 5 6 10 90 32] [18 7 10 8 13] [10 20 30 10 7]]

DEMONSTRATION

Browser other questions tagged

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