5

I've got a list of objects:

array = [object0,object1,object2,object3,object4]

and i want to change the order of the items given a permutation:

permutation = [ 2 , 4 , 0 , 1 , 3 ]

Is there a command in python that will do something like:

result = Permute(array,permutation)

result = [object2,object4,object0,object1,object3]

I know i can do it with a simple for loop....

Yochai Timmer
  • 48,127
  • 24
  • 147
  • 185
  • Possible duplicate of https://stackoverflow.com/questions/6098250/in-place-way-to-apply-a-permutation-to-a-list-inverse-of-sorting-by-key – user202729 Aug 17 '19 at 07:31

5 Answers5

8

If we are assuming that permutation is a proper permutation of 0-n (each appears exactly once), then the following code should work:

result=[array[i] for i in permutation]
murgatroid99
  • 19,007
  • 10
  • 60
  • 95
5

In Python, this is easy to do with a list comprehension:

result = [array[i] for i in permutation]
John Bartholomew
  • 6,428
  • 1
  • 30
  • 39
3

Just for the sake of completeness a version with no for at all:

seed = ['foo', 'bar', 'baz']
permutation = [1, 2, 0]
result = map(lambda i: seed[i], permutation)
print result # --> ['bar', 'baz', 'foo']

I'd rather stick with the list comprehension guys, though. ;)

jena
  • 8,096
  • 1
  • 24
  • 23
0

Use shuffle method from numpy

import numpy as np
arr = np.arange(10)
np.random.shuffle(arr)
print(arr)

[1 7 5 2 9 4 3 6 0 8]

Reference: https://docs.scipy.org/doc/numpy-1.15.0/reference/generated/numpy.random.shuffle.html

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Farhad Maleki
  • 3,451
  • 1
  • 25
  • 20
0

You can use index swapping. You a have two array a and b

def swap_random(a, b):
"""Randomly swap entries in two arrays."""
# Indices to swap
    swap_inds = np.random.random(size=len(a)) < 0.5 # your threshold 

# Make copies of arrays a and b for output
    a_out = np.copy(a)
    b_out = np.copy(b)

# Swap values
   a_out[swap_inds] = b[swap_inds]
   b_out[swap_inds] = a[swap_inds]

   return a_out, b_out

So, do the test

d = np.array(range(0,15))
r = np.array(range(16,31))

display(d,r)

>>> array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14])
>>> array([16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30])


display(swap_random(d, r))
>>> (array([ 0, 17,  2,  3, 20, 21, 22,  7, 24, 25, 10, 11, 28, 13, 14]),
>>> array([16,  1, 18, 19,  4,  5,  6, 23,  8,  9, 26, 27, 12, 29, 30]))
  • Hi! While numpy is often a good solution for anything related to big arrays of numbers, I think this answer would be more helpful if the first sentence was something along the lines of "Consider using numpy for this", and then you code began with `import numpy as np`. Otherwise, your answer only makes sense for those who are already familiar enough with numpy. – Stef Mar 05 '23 at 18:10
  • Also, on closer look, it's not obvious what's the relation between the function `swap_random` you propose and the OP's question about applying a permutation. In fact, applying a permutation is extremely easy in numpy, using the permutation as the index for the other array: `def apply_permutation(perm, arr): return np.array(arr)[perm]` – Stef Mar 05 '23 at 18:13