开发者

How can I sort a portion of a list in place? [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Python: sort a part of a list, in place

I want to implement a decision tree algorithm, and my implementation calls for sorting the table in order attribute by attribute, in place.

Here is the gist of it: choose an attribute, sort over it. All entries in the table that share that attribute can no开发者_如何学编程w be thought of as another list, because permuting within them does not modify that attribute.

So then, how can I sort just that relevant subportion? Do I need to write my own wrapper around the list with a specified base and length?


This is sort-of inplace. It does require temporary storage for the sorted part

>>> a=range(20,0,-1)
>>> a
[20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
>>> a[10:15]=sorted(a[10:15])
>>> a
[20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 6, 7, 8, 9, 10, 5, 4, 3, 2, 1]
>>> 
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜