Efficient Datatype Python (list or numpy array?)
I'm still con开发者_如何学Pythonfused whether to use list or numpy array.
I started with the latter, but since I have to do a lot of append I ended up with many vstacks slowing my code down. Using list would solve this problem, but I also need to delete elements which again works well with delete on numpy array.As it looks now I'll have to write my own data type (in a compiled language, and wrap). I'm just curious if there isn't a way to get the job done using a python type.
To summarize this are the criterions my data type would have to fulfil:
- 2d n (variable) rows, each row k (fixed) elements
- in memory in one piece (would be nice for efficient operating)
- append row (with an in average constant time, like C++ vector just always k elements)
- delete a set of elements (best: inplace, keep free space at the end for later append)
- access element given the row and column index ( O(1) like data[row*k+ column]
It appears generally useful to me to have a data type like this and not impossible to implement in C/Fortran.
What would be the closest I could get with python? (Or maybe, Do you think it would work to write a python class for the datatype? what performance should I expect in this case?)As I see it, if you were doing this in C or Fortran, you'd have to have an idea of the size of the array so that you can allocate the correct amount of memory (ignoring realloc
!). So assuming you do know this, why do you need to append to the array?
In any case, numpy arrays have the resize
method, which you can use to extend the size of the array.
精彩评论