Home > Enterprise >  IndexError is being generated when deleting first and last terms in a Numpy array
IndexError is being generated when deleting first and last terms in a Numpy array

Time:11-24

I have a simple program that I'm writing for a math class that generates an array of numbers from 1 to 10. After removing the 10s I would like to count and remove pairs of numbers that add up to at least 10, therefore I order the array in ascending order and check if the sum of the first and last numbers is greater than or equal to 10. If the sum >= 10, I want to delete those numbers using numpy.delete.

What I don't understand is that sometimes the code works and other times I get an error: IndexError: index 0 is out of bounds for axis 0 with size 0. The error usually refers to the lines that have to do with sum or the numpy.delete operations.

Here is the code:

import numpy as np

no_of_dice = 10
dice = []

dice = np.random.randint(1, high=11, size=no_of_dice)
print(dice)
no_of_tens = np.count_nonzero(dice == 10)
print("Number of 10s:", no_of_tens)
dice = np.delete(dice, np.where(dice == 10))
dice = np.sort(dice)
print(dice.shape, dice)

no_of_pairs = 0
sum = dice[0]   dice[-1]

while sum >= 10:
    dice = np.delete(dice, -1)
    dice = np.delete(dice, 0)
    sum = dice[0]   dice[-1]
    no_of_pairs  = 1

print(dice)
print("Number of pairs:", no_of_pairs)

CodePudding user response:

I checked and tested your code and I found two problems. First, let's see about the both errors that you mentioned

a) index 0 is out of bounds for axis 0 with size 0 on sum = dice[0]

On that case, I tried to print dice to check if dice was empty (since that error refers that you tried to get an element from that list with an index bigger than the length of the list)

print(dice) #The command i used
>>>>[] #What the program returned

As you see, the list was empty. Why was it empty? Let's check the procedure of your program. For that, I added and print(dice) instruction every time it iterates on the while sum>=10

#--------------Your code------------------------------
while sum >= 10:
    dice = np.delete(dice, -1)
    dice = np.delete(dice, 0)
    sum = dice[0]   dice[-1]
    no_of_pairs  = 1

#------------------Prints i added--------------------
while sum >= 10:
    dice = np.delete(dice, -1)
    dice = np.delete(dice, 0)
    sum = dice[0]   dice[-1]
    no_of_pairs  = 1
    print(dice) #-----------print i added
    print("Denso") #----------Print i added

This was the result

[5 6 2 7 2 8 5 9 5 7]
Number of 10s: 0
(10,) [2 2 5 5 5 6 7 7 8 9]
[2 5 5 5 6 7 7 8]
Denso
[5 5 5 6 7 7]
Denso
[5 5 6 7]
Denso
[5 6]
Denso

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
Input In [55], in <cell line: 17>()
     18 dice = np.delete(dice, -1)
     19 dice = np.delete(dice, 0)
---> 20 sum = dice[0]   dice[-1]
     21 no_of_pairs  = 1
     22 print(dice)
IndexError: index 0 is out of bounds for axis 0 with size 0

So what happened? before the crash, you see that dice only had two elements: 5 and 6, and both of them sum 11, so sum was greather than 10. Then, the while executed again. However, if you notice your code, you execute two np.delete on the first and last elements. So, that means that you deleted, in this case, 5 and 6, which where the only two elements remaining on the list. After that, you try to execute this:

sum = dice[0]   dice[-1]

And that gives you the crash, why? because you deleted 5 and 6, which where the remaining elements of the list. With that, dice becomes empty, and trying to do dice[0] when the list is empty crashes the program. To solve that, a suggestion is to check after the delete instruction whereas the list is empty or not to forcebreak the while loop and avoid that crash

Now for the second case

b) index 0 is out of bounds for axis 0 with size 0 on np.delete(dice, 0)

I was able to reproduce the error. Considering the previous change i did, this was the result

[10  9  6  7  6  2  3  2  7  9]
Number of 10s: 1
(9,) [2 2 3 6 6 7 7 9 9]
[2 3 6 6 7 7 9]
Denso
[3 6 6 7 7]
Denso
[6 6 7]
Denso
[6]
Denso

IndexError                                Traceback (most recent call last)
Input In [44], in <cell line: 17>()
     17 while sum >= 10:
     18     dice = np.delete(dice, -1)
---> 19     dice = np.delete(dice, 0)
     20     sum = dice[0]   dice[-1]
     21     no_of_pairs  = 1
[.....]
IndexError: index 0 is out of bounds for axis 0 with size 0

The problem here is the same, but if you notice, now there was only one element remaining on dice before the crash instead of two.

I executed the print instruction again

print(dice) #The command i used
>>>>[] #What the program returned

Again, dice was empty, How so? The problem was similars to the first case. If you notice, 6 was the last element before the iteration on sum>=10 restarts (and it restarts because the sum is 12, why? because dice[0] and dice [-1] refers to the same number, 6 (is the only element on the list)). So, you execute, again, two deletes on the dice list. However, you delete the last and first element of the list. The problem is that, on the first delete, you erase the last remaining element on the list. Then, you try to execute the delete command again, but since the list is empty, it crashes. It doesn't have anything to delete. A suggestion i make you is to check whereas (on the while conditional) dice has 1 element or less. With that, once you have 1 element remaining, it will break the while loop

Hopefully it helps you

The second problem (besides your question) is that your method itself doesn't solve the issue (delete the pairs that sum 10 or more). Take a look on this example result of your program on a iteration it worked

[10  3  6  2  3  4 10  3  5  8]
Number of 10s: 2
(8,) [2 3 3 3 4 5 6 8]
[3 3 3 4 5 6]
Number of pairs: 1

You can see that it removed the 2-8 pair, which is correct (both sum 10). However, it didn't remove the 4-6 pair. That is because on your for-while loop, once it checked that the first and last element (3 and 6) sum 9, it broke the while loop, and didn't checked for 4-6 pair for example. My suggestion for that is, instead of suming the first and last element, you compare the first element to every element on the list and if you find a sum which goes to 10 or more, delete those. Then check the second element of the list, and so on. There are different ways to solve that issue, mine is one of them, probably you can make your own too.

  • Related