简体   繁体   中英

IndexError: list index out of range, when i create a matrix

I am working on a homework program, where I need to create a matrix, I did the function that takes an array with some records (that I previously create) as a parameter, but when I create a high number of records, for instance: 100 records, I get the list index out of range

array creation:

def crearVector(n):
    vec = [None] * n
    for i in range(len(vec)):
        codigo = random.randint(1, 1000)
        precio = round(random.uniform(1, 100000), 2)
        ubicacion = random.randint(1, 23)
        estado = random.randint(0, 1)
        cantidad = random.randint(0, 1000)
        puntuacion = random.randint(1, 5)
        publicacion = Publicacion(codigo, precio, ubicacion, estado, cantidad, puntuacion)
        vec[i] = publicacion
    return vec

array creation function calling:

def test():
    n = validateHigherThan(0)
    vec = crearVector(n)

matrix creation and showing function:

mat = crearMatriz(vec)
forma = int(input("How would you like to show the matrix?(0: matrix, 1: list): "))
if forma == 1:
    mostrarMatrizLista(mat)
elif forma == 0:
     mostrarMatriz(mat)

matrix creation:

def crearMatriz(vec):
    mat = [[0] * 5 for i in range(23)]
    for i in range(len(vec)):
        fil = vec[i].ubicacion
        col = vec[i].puntuacion-1
        mat[fil][col] += 1
    return mat

You create a matrix of 23 * 5

fil = vec[i].ubicacion

should be

fil = vec[i].ubicacion-1

since ubicacion can have random numbers from 1 to 23.

Better yet you could use global variables to define those parameters instead of hardcoding them in both functions.

col_limit = 5
fil_limit = 23
def crearVector(n):
    vec = [None] * n
    for i in range(len(vec)):
        codigo = random.randint(1, 1000)
        precio = round(random.uniform(1, 100000), 2)
        ubicacion = random.randint(1, fil_limit)
        estado = random.randint(0, 1)
        cantidad = random.randint(0, 1000)
        puntuacion = random.randint(1, col_limit)
        vec[i] = Publicacion(codigo, precio, ubicacion, estado, cantidad, puntuacion)
    return vec

def crearMatriz(vec):
    mat = [[0] * col_limit for i in range(fil_limit)]
    for i in range(len(vec)):
        fil = vec[i].ubicacion - 1
        col = vec[i].puntuacion- 1
        mat[fil][col] += 1
    return mat

I believe you're missing a -1 on the ubicacion for your fil column on the matrix. Please see the code below. Be careful when you define columns that start with 0s.

# ubicacion = random.randint(1, 23) (range is 1 to 23)
def crearMatriz(vec):
    mat = [[0] * 5 for i in range(23)]
    for i in range(len(vec)):
        fil = vec[i].ubicacion-1 # <- this -1 is what you were missing, when ubicacion = 23 list goes out of range.
        col = vec[i].puntuacion-1        
        mat[fil][col] += 1
    return mat

Also, I assume your program does not really need legibility as much, since it is a homework assignment, however, using some variables to help you define your max ranges is useful to help you debug, eg.

import random

CODIGO_MAX = 1000
PRECIO_MAX = 100000
UBICACION_MAX = 23
ESTADO_MAX = 1
CANTIDAD_MAX = 1000
PUNTUACION_MAX = 5

def crearVector(n):
    vec = [None] * n
    for i in range(len(vec)):
        codigo = random.randint(1, CODIGO_MAX)
        precio = round(random.uniform(1, PRECIO_MAX), 2)
        ubicacion = random.randint(1, UBICACION_MAX)
        estado = random.randint(0, ESTADO_MAX)
        cantidad = random.randint(0, CANTIDAD_MAX)
        puntuacion = random.randint(1, PUNTUACION_MAX)
        publicacion = Publicacion(codigo, precio, ubicacion, estado, cantidad, puntuacion)
        vec[i] = publicacion
    return vec


def crearMatriz(vec):
    mat = [[0] * PUNTUACION_MAX for i in range(UBICACION_MAX)]
    for i in range(len(vec)):
        fil = vec[i].ubicacion-1
        col = vec[i].puntuacion-1
        mat[fil][col] += 1
    return mat



crearMatriz(crearVector(1000))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM