Problem when deleting a number from a list!

Asked

Viewed 178 times

-2

After drawing a number from a list, I need to delete this number from my list. but the message "list index out of range" appears. My error is in the if line. or is there a better way to delete that number? remembering that the other numbers that were not drawn have to stay on the list to be drawn. I have studied the functions of the module Random, but none fits to solve this problem.

import random
import time
Matriz = []
Tamanho_Que_Ficou = 20
Tamanho = 21
Lista_De_Numeros = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21]
for i in range(3):
    Matriz.append([0] * 7)

for i in range(3):
    for j in range(7):
        Numero = random.choice(Lista_De_Numeros)
        Matriz[i][j] = Numero
        for k in range(Tamanho):
            if Numero == Lista_De_Numeros[k]:
                del Lista_De_Numeros[k]
                Lista_De_Numeros = [] * Tamanho_Que_Ficou
                Tamanho_Que_Ficou = Tamanho_Que_Ficou - 1
                Tamanho = Tamanho - 1
print(Matriz)

1 answer

2


The mistake IndexError is being generated because you modify inside the block if the size of the list inside the block for. When the program gets a random number, it deletes it from the original list and after that it proceeds with the for loop, where the range is set to the list size before having the number deleted.

The solution to solve this error was to add break at the end of the block if. That way he would stop the execution of the block for. The problem is that after that it generates the following error:

Indexerror: Cannot Choose from an Empty Sequence

This error occurs because on the line Lista_De_Numeros = [] * Tamanho_Que_Ficou your number list is empty. I didn’t understand why you wrote this, but to correct this error, I needed to delete that line.

The corrected code looked like this:

import random
import time

Matriz = []
Tamanho = 21
Lista_De_Numeros = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21]

for i in range(3):
    Matriz.append([0] * 7)

for i in range(3):
    for j in range(7):

        Numero = random.choice(Lista_De_Numeros)
        Matriz[i][j] = Numero

        for k in range(Tamanho):
            if Numero == Lista_De_Numeros[k]:

                del Lista_De_Numeros[k]
                Tamanho = Tamanho - 1

                break
print(Matriz)

There are also some things you can do to decrease this code.

  • You do not need to check number by number to finally delete the value obtained from the list. There is a list method called remove(value) deleting the value from your list.

  • You can use list comprehension so you don’t have to type the numbers from the list of numbers manually.

Improved code:

import random
import time

Tamanho = 21

Matriz = [[0]* 7 for i in range(3)]
Lista_De_Numeros = [number + 1 for number in range(Tamanho)]

for i in range(3):
    for j in range(7):

        Numero = random.choice(Lista_De_Numeros)
        Matriz[i][j] = Numero
        Lista_De_Numeros.remove(Numero)

print(Matriz)
  • I’m a beginner in python, but your code was much more practical. I searched and did not find this command "remove". Thanks!

Browser other questions tagged

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