I am working on an epidemic simulator. However, I have run into a problem: I need to create a matrix of my custom class Location() which has a init() method of __init(self, x_coords, y_coords, population, name)__. All of these parameters are supposed to be random (excluding the name).
The only problem I encounter is when trying to create a matrix / map of these custom locations which I would use in my simulation.
I have tried:
num_rows = 5
num_cols = 5
row = [Location() for i in range(num_rows)]
mat = [list(row) for i in range(num_cols)]
mat[0][0] = Location(...)
num_rows = 5
num_cols = 5
row = [Location() for i in range(num_rows)]
mat = [list(row) for i in range(num_cols)]
mat.append(Location(...))
w, h = 5, 5
Matrix = [[Location() for x in range(w)] for y in range(h)]
Matrix.append(Location(...))
w, h = 5, 5
Matrix = [[Location() for x in range(w)] for y in range(h)]
Matrix[0][0] = Location(...)
All of these encounter the same type error: "Expected type 'List[Location]' (matched generic type '_T'), got 'Location' instead."
I have not been working with Python for very long so I do not know of any workarounds to this and I have found nothing online to help with this.
My full code currently looks like this:
import random
# Location class
class Location:
def __init__(self, x_coords, y_coords, population, name):
self._x_coords = x_coords
self._y_coords = y_coords
self._population = population
self._name = name
# GETTERS VVVV
def get_x_coords(self):
return self._x_coords
def get_y_coords(self):
return self._y_coords
def get_population(self):
return self._population
def get_name(self):
return self._name
# Need to initialize a matrix of Location() here
ctrl-k)def __index__(self, x_coords, y_coords, population, name):- is that a typo? Should it bedef __init__(self, x_coords, y_coords, population, name):?