I’m looking to create a 2d matrix of integers with symmetric addressing ( i.e. matrix[2,3] and matrix[3,2] will return the same value ) in python. The integers will have addition and subtraction done on them, and be used for logical comparisons. My initial idea was to create the integer objects up front and try to fill a list of lists with some python equivalent of pointers. I’m not sure how to do it, though. What is the best way to implement this, and should I be using lists or another data structure?
Share
A simpler and cleaner way is to just use a dictionary with sorted tuples as keys. The tuples correspond with your matrix index. Override
__getitem__and__setitem__to access the dictionary by sorted tuples; here’s an example class:And then use it like this: