5

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 now 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?

Community
  • 1
  • 1
alexgolec
  • 26,898
  • 33
  • 107
  • 159
  • What do you mean with a portions? You mean a slice from item position X to item position Y? –  Apr 29 '11 at 03:46
  • This doesn't seem like an exact duplicate of "sort part of a list in place" to me. It sounds more like he wants a projection / view showing a subset of his data, filtered by and sorted on a given attribute. – samplebias Apr 29 '11 at 04:11

1 Answers1

9

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]
>>> 
John La Rooy
  • 295,403
  • 53
  • 369
  • 502