Yesterday I had the need for a matrix type in python.
Apparently, a trivial answer to this need would be to use numpy.matrix(), but the additional issue I have is that I would like a matrix to store arbitrary values with mixed types, similarly to a list. numpy.matrix does not perform this. An example is
>>> numpy.matrix([[1,2,3],[4,"5",6]])
matrix([['1', '2', '3'],
['4', '5', '6']],
dtype='|S4')
>>> numpy.matrix([[1,2,3],[4,5,6]])
matrix([[1, 2, 3],
[4, 5, 6]])
As you can see, the numpy.matrix must be homogeneous in content. If a string value is present in my initialization, every value gets implicitly stored as a string. This is also confirmed by accessing the single values
>>> numpy.matrix([[1,2,3],[4,"5",6]])[1,1]
'5'
>>> numpy.matrix([[1,2,3],[4,"5",6]])[1,2]
'6'
Now, the python list type can instead accept mixed types. You can have a list containing an integer and a string, both conserving their type. What I would need is something similar to a list, but operating in a matrix-like behavior.
Therefore, I had to implement my own type. I had two choices for the internal implementation: list containing lists, and dictionaries. Both solutions have shortcomings:
- list of lists require careful synchronization of the various lists' sizes. Swapping two rows is easy. Swapping two columns is less easy. Removing a row is easy as well.
- dictionaries (with a tuple as a key) are slightly better, but you have to define the limits of your key (eg. you cannot insert element 5,5 if your matrix is 3x3), and they are more complex to use to insert, remove, or swap columns or rows.
Edit: clarification. The concrete reason on why I need this functionality is because I am reading csv files. Once I collect the values from a csv files (values that can be string, integers, floats) I would like to perform swapping, removal, insertion and other operations alike. For this reason I need a "matrix list".
My curiosities are:
- do you know if a python data type providing this service already exists (maybe in a "non-battery included" library out there) ?
- why is this data type not provided in the standard library ? Too restricted interest maybe?
- How would you have solved this need ? dictionary, list, other smarter solution ?