tags:

views:

602

answers:

8

If I have a list [a,b,c,d,e] how can I reorder the items in an arbitrary manner like [d,c,a,b,e]?

Edit: I don't want to shuffle them. I want to re-order them in a predefined manner. (for example, I know that the 3rd element in the old list should become the first element in the new list)

+15  A: 

See random.shuffle()


UPDATE: Based on your edit (you don't want to shuffle...) try this:

mylist=['a','b','c','d','e']
myorder=[3,2,0,1,4]
mylist = [ mylist[i] for i in myorder]
print mylist
AJ
Would really appreciate some comments on these sudden downvotes...what gives?
AJ
not only the downvotes, you are also marked as spam... bah..
Stefano Borini
@Stefano - "marked as spam" what do you mean? how can i see this?
AJ
only +10k can see it. don't worry. Someone flagged your post as spam. Don't ask.
Stefano Borini
+9  A: 
>>> import random
>>> x = [1,2,3,4,5]
>>> random.shuffle(x)
>>> x
[5, 2, 4, 3, 1]
Mark
+1  A: 
>>> a = [1, 2, 3]
>>> a[0], a[2] = a[2], a[0]
>>> a
[3, 2, 1]
SilentGhost
+1  A: 
>>> a=["a","b","c","d","e"]
>>> a[0],a[3] = a[3],a[0]
>>> a
['d', 'b', 'c', 'a', 'e']
ghostdog74
A: 

Is the final order defined by a list of indices ?

>>> items = [1, None, "chicken", int]
>>> order = [3, 0, 1, 2]

>>> ordered_list = [items[i] for i in order]
>>> ordered_list
[<type 'int'>, 1, None, 'chicken']

edit: meh. AJ was faster... http://stackoverflow.com/questions/2177590/how-can-i-reorder-a-list-in-python/2177607#2177607

RaphaelSP
A: 
newList = [oldList[3]]
newList.extend(oldList[:3])
newList.extend(oldList[4:])
inspectorG4dget
+1  A: 

You can provide your own sort function to list.sort():

The sort() method takes optional arguments for controlling the comparisons.

  • cmp specifies a custom comparison function of two arguments (list items) which should return a negative, zero or positive number depending on whether the first argument is considered smaller than, equal to, or larger than the second argument: cmp=lambda x,y: cmp(x.lower(), y.lower()). The default value is None.

  • key specifies a function of one argument that is used to extract a comparison key from each list element: key=str.lower. The default value is None.

  • reverse is a boolean value. If set to True, then the list elements are sorted as if each comparison were reversed.

In general, the key and reverse conversion processes are much faster than specifying an equivalent cmp function. This is because cmp is called multiple times for each list element while key and reverse touch each element only once.

Felix Kling
and how exactly you'd implement this?
SilentGhost
@SilentGhost: This is meant as a general answer. In the OPs case your answer is more appropriate. Nevertheless I think it is important to know that a generic solution exists.
Felix Kling
A: 

From what I understand of your question, it appears that you want to apply a permutation that you specify on a list. This is done by specifying another list (lets call it p) that holds the indices of the elements of the original list that should appear in the permuted list. You then use p to make a new list by simply substituting the element at each position by that whose index is in that position in p.

def apply_permutation(lst, p):
    return [lst[x] for x in p]

arr=list("abcde")
new_order=[3,2,0,1,4]

print apply_permutation(arr,new_order)

This prints ['d', 'c', 'a', 'b', 'e'].

This actually creates a new list, but it can be trivially modified to permute the original "in place".

MAK