0

The following function is placed in a for loop, where because of the for loop indices can get high negative values as in the following example:

C_ki[2][1] = max (C_ki[1][1], C_ki[3][-7] , C_ki[2][0])

# C_ki is defined as:

C_ki = [[0]*(7) for x in range(3)]

C_ik = [[0]*(3) for x in range(7)]

For calculating the above C_ki[2][1], python gives an error list index out of range because of the negative index. Is there a manner to set C_ki[3][-7] to zero for negative indices or do not take it into account for the max function?

Stijn
  • 33
  • 6
  • You cannot do `len(3)` in python. Also, the curly brackets cannot be used in this case. You should use these `()` or `[]`. – Vlad Mar 13 '19 at 11:58
  • `max()` is a function, you need to put your set into parentheses, like this : ``max({C_ki[1][1], C_ki[3][-7] , C_ki[2][0]})`` – Fukiyel Mar 13 '19 at 12:00
  • But why can't you just.. replace the ``-7`` with ``0`` ? Do you need to replace it **under a certain condition**, like if it exists ? – Fukiyel Mar 13 '19 at 12:04
  • There is a formula that has to taken in to acount, that sometimes give the negative values. – Stijn Mar 13 '19 at 12:06

5 Answers5

3

You can wrap the extraction in a function,

def get_or_zero(array, i, j):
    try:
        return array[i][j]
    except IndexError:
        return 0

then you can use get_or_zero(C_ki, i, j) instead of C_ki[i][j] in the max function.

vlizana
  • 2,962
  • 1
  • 16
  • 26
  • did you try for example: get_or_zero(C_ki,1,-4) ? if index is negative its indexing reversed, it will not gave an error if index is in range @vlizan – ncica Mar 13 '19 at 12:46
1

If you want to put the index -7 only if it exists, and else the index 0, you can use the ternary operator :

C_ki[2][1] = max(C_ki[1][1], C_ki[3][-7 if len(C_ki[3]) >= 7 else 0] , C_ki[2][0])

Elif you want to put the index -7 only if it exists, and else the value 0 :

C_ki[2][1] = max(C_ki[1][1], C_ki[3][-7] if len(C_ki[3]) >= 7 else 0, C_ki[2][0])

More on the ternary operator

Fukiyel
  • 1,166
  • 7
  • 19
0

So, if i is your (possibly) negative index you can simply do something like this

C_ki[2][1] = max (C_ki[1][1], C_ki[3][max(i,0)] , C_ki[2][0])

If, as I suspect, you want the whole element value set to zero than you can use a conditional statement

C_ki[2][1] = max (C_ki[1][1], 0 if i <0 else C_ki[3][i] , C_ki[2][0])
CAPSLOCK
  • 6,243
  • 3
  • 33
  • 56
0

Note: you are getting index error because list index out of range (C_ki[3][-7])

your C_ki have dimension [3][7] with index start counting from 0 to 2. 3 is out of range.

NOTE: what happening with negative indexes:

example:

array = [0,1,2,3,4,5]
print (array[-1]) # equal array[5]

result: 5

ncica
  • 7,015
  • 1
  • 15
  • 37
0

9 times out of ten it is better to fix the problem with the indices but assuming this is the 10th time, the built in min, max functions can be combined to give you the kind of protection you are looking for:

# legal value, no change required
I= 2
D=(min(max(I,0),4))
# D will equal 2

# large negative set to zero
I= -7
D=(min(max(I,0),4))
# D will zero 0

# large positive set to the value of the maximum of 4
I= 7
D=(min(max(I,0),4))
# D will equal 4