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

1

Hello, how do I change the value of a number in an array when I do not know what its index and can there be more than one value to change? For example, if I have the array:

[[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 values that are less than or equal to 4 to be changed to 10, how do I? Specifically a repeat structure, since the array may have different size depending on the case. I tried to do it as follows:

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 repeat 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]]
    
asked by anonymous 01.04.2018 / 07:49

2 answers

1

The .index () list will return only the lowest 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)
    
01.04.2018 / 14:31
1
  • The numero <= 4 line does not make much sense, it's doing absolutely nothing.
  • Remember that you have a list of lists, so the logic inside the for is not correct, you must access two indices, the sublist and the number within the southern one so you can compare > / change its value.
  • lista.index(...) returns only the index of the first occurrence of a given element (4, in this case) within a list
  • Using for cycle, enumerate and doing it the 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]]
    
    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]]
    

    STATEMENT

    Using list comprehension can 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]]
    

    STATEMENT

        
    01.04.2018 / 18:43