如何在Python中就地对整数数组进行排序? [英] How to sort an integer array in-place in Python?
本文介绍了如何在Python中就地对整数数组进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在Python 2.6中就地对整数数组(不是列表)进行排序?标准库之一中是否有合适的功能?
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])
提前谢谢!
推荐答案
好吧,您不能使用array.array
来做到这一点,但是可以使用numpy.array
:
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])
或者您可以直接从array.array
进行转换:
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)
这篇关于如何在Python中就地对整数数组进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文