I’m looking for dynamically growing vectors in Python, since I don’t know their length in advance. In addition, I would like to calculate distances between these sparse vectors, preferably using the distance functions in scipy.spatial.distance (although any other suggestions are welcome). Any ideas how to do this? (Initially, it doesn’t need to be efficient.)
Thanks a lot in advance!
You can use regular python lists (which are dynamic) as vectors. Trivial example follows.
As per aganders3’s suggestion, do note that you can also use numpy arrays if needed:
If the sparse part of your question is crucial I’d use scipy for that – it has support for sparse matrixes. You can define a 1xn matrix and use it as a vector. This works (the parameter is the size of the matrix, filled with zeroes by default):
There are many kinds of sparse matrixes, some dictionary based (see comment). You can define a row sparse matrix from a list like this: