0

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
7
  • I know you're not asking for a code review, but getters are not very Pythonic, consider using the @property decorator. To answer your question remove list from around row which is giving you a list of list(list(location)) as the error states you want a list of list(location). Commented Apr 8, 2020 at 17:14
  • When posting a question about code that produces an Exception, always include the complete Traceback - copy and paste it then format it as code (select it and type ctrl-k) Commented Apr 8, 2020 at 17:40
  • I tried a few of your examples and None of them produced an error. Commented Apr 8, 2020 at 17:44
  • def __index__(self, x_coords, y_coords, population, name): - is that a typo? Should it be def __init__(self, x_coords, y_coords, population, name):? Commented Apr 8, 2020 at 17:46
  • Oh, just added that as an answer (__ index __ vs __ init __ ) :) Oh, well. Good that you found it. Good luck with the simulation. Any plans to publish the results somewhere? Commented Apr 8, 2020 at 17:56

2 Answers 2

1

I believe what you want is something like this

num_col = 5
num_row = 5
mat = [[] for i in range(num_row)] # create a list of empty lists

for row in range(num_row):
    for col in range(num_col):
        mat[row].append(Location(...)) # populate each list with Locations
Sign up to request clarification or add additional context in comments.

Comments

1

You need to implement __init__(self, x_coords, y_coords, population, name) in your Location class instead of __index__(self, x_coords, y_coords, population, name).

Here is the code (Location constructor parameters simplified):

num_rows = 5
num_cols = 5

class Location:
    def __index__(self, locationIndex=0):
        print( "Location::__index__(" + str( locationIndex) + ")")

    def __init__(self, locationIndex=0):
        print( "Location::__init__(" + str( locationIndex) + ")")

    row = [ Location(i) for i in range( 0, num_rows) ]
    mat = [ list(row) for i in range( 0, num_cols)]

    mat[0][0] = Location(...)
    print( mat)

and the output is:

Location::__init__(0)
Location::__init__(1)
Location::__init__(2)
Location::__init__(3)
Location::__init__(4)
Location::__init__(Ellipsis)

Note, not Location::__index__() but Location::__init__() will be called up on the list comprehension.

The last call Location::__init__(Ellipsis) comes from print( mat).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.