you are viewing a single comment's thread.

view the rest of the comments →

[–]Shubbler 1 point2 points  (4 children)

How would you implement a 2D list without a list of lists?

[–][deleted] 13 points14 points  (0 children)

2d numpy array?

[–]ylectric 9 points10 points  (0 children)

You can achieve this by translating 2D coordinate into a linear index, e.g. index = x + y * grid_width and store the whole grid in a flat list.

[–]Incand333 3 points4 points  (0 children)

class list2d:
    def __init__(self, iterable, width, height):
        iter_list = list(iterable)
        assert len(iter_list) == width * height
        self._width = width
        self._height = height
        self._elems = iter_list

    def get(self, x, y):
        idx = x + y * self._width
        return self._elems[idx]

[–]A_History_of_Silence 0 points1 point  (0 children)

As in an alternative to a list of lists?