how can one sort an integer array (not a list) in-place in Python 2.6? Is there a suitable function in one of the standard libraries?
In other words, I'm looking for a function that would do something like this:
>>> a = array.array('i', [1, 3, 2])
>>> some_function(a)
>>> a
array('i', [1, 2, 3])
Thanks in advance!
解决方案
Well, you can't do it with array.array, but you can with numpy.array:
In [3]: a = numpy.array([0,1,3,2], dtype=numpy.int)
In [4]: a.sort()
In [5]: a
Out[5]: array([0, 1, 2, 3])
Or you can convert directly from an array.array if you have that already:
a = array.array('i', [1, 3, 2])
a = numpy.array(a)