Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 就地排序的子列表_Python_List_Sorting_View_Slice - Fatal编程技术网

Python 就地排序的子列表

Python 就地排序的子列表,python,list,sorting,view,slice,Python,List,Sorting,View,Slice,可能吗?显然x[1:-1].sort()不起作用,因为切片是副本 当前解决方法: >>> x = [4, 3, 1, 2, 5] >>> s = slice(1, -1) >>> x[s] = sorted(x[s]) >>> x [4, 1, 2, 3, 5] 我能在python列表上找到一个视图吗 如果是一个选项: >>> x = np.array([1, 8, 90, 30, 5]) >>

可能吗?显然
x[1:-1].sort()
不起作用,因为切片是副本

当前解决方法:

>>> x = [4, 3, 1, 2, 5]
>>> s = slice(1, -1)
>>> x[s] = sorted(x[s])
>>> x
[4, 1, 2, 3, 5]
我能在python列表上找到一个视图吗

如果是一个选项:

>>> x = np.array([1, 8, 90, 30, 5])
>>> x[2:].sort()
>>> x
array([ 1,  8,  5, 30, 90])

numpy
array始终是原始数组的视图。

Related:请参见现在的视图,查看python中的列表,afaik,因为它存在于dict(dict.viewkey等)中。我没有看到列表中视图的任何用例,那么视图是什么意思&它应该提供什么?(
b=x
将是某种视图,尽管它非常无用)。
x[1:-1]=sorted(x[1:-1])
在我看来是非常整洁的。你为什么不喜欢它?我想你是对的。获取切片是O(n),但它是无关的,因为排序无论如何都是O(n*log(n))