开发者

Python: memory efficient list with list.sort(cmp=myfnc)

What is the best way to improve this code:

def my_func(开发者_如何学Pythonx, y):
    ... do smth ...
    return cmp(x',y') 

my_list = range(0, N)
my_list.sort(cmp=my_func)

A python's list takes a lot of memory in comparison with numpy array (6800MB vs 700MB), but nympy.array doesn't have the sort function with cmp argument.

Are there other ways to improve memory usage or sort numpy's array with my cmp function?

Update: my current solution is a C function (shared with SWIG) that sorts a huge array of integers and returns it to python after sorting.

But I hope that there is some way to implement memory efficient sorting of huge datasets with Python. Any ideas?


If you can write a ufunc that convert your array, you can do fast sort by argsort:

b = convert(a)
idx = np.argsort(b)
sort_a = a[idx]


As an alternative you can use the builtin sorted with an numpy array:

>>> a = np.arange(10, 1, -1)
>>> sorted(a, cmp=lambda a,b: cmp(a,b))
[2, 3, 4, 5, 6, 7, 8, 9, 10]

It is not in-place, so you have 1400 MB compared to 6800 MB.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜