Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

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?

like image 619
alexgolec Avatar asked Apr 29 '11 03:04

alexgolec


People also ask

How do I sort a specific part of a list in Python?

int * ptr = array; std::sort( ptr + 1, ptr + 4 );

How do you sort duplicates in Python?

Method #1 : Using count() + set() + sorted() The sorted function can be used to sort the elements as desired, the frequency can be computed using the count function and removal of duplicates can be handled using the set function.

How do you sort a list with respect to another list?

Approach : Zip the two lists. Create a new, sorted list based on the zip using sorted(). Using a list comprehension extract the first elements of each pair from the sorted, zipped list.


1 Answers

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]
>>> 
like image 88
John La Rooy Avatar answered Oct 23 '22 14:10

John La Rooy