0

I'm trying to change the the position of the elements of a python list according to an array which tells where each element must be placed.

For example consider a list ["a","b","c"], for these array values [2,0,1]=> the element at index position 0 (a) must go to index position 2, element at index position 1 (b) go to index position 0 and element at index position 2 go to index position 1.

The expected list should look like this ["b","c","a"],

Georgy
  • 12,464
  • 7
  • 65
  • 73
  • 4
    Does this answer your question? [How can I reorder a list?](https://stackoverflow.com/questions/2177590/how-can-i-reorder-a-list) Not the best duplicate target but the accepted answer is what you are looking for. – Georgy Jul 17 '20 at 17:13
  • Array rotation, https://stackoverflow.com/a/17350541/4985099 – sushanth Jul 17 '20 at 17:14
  • How did you end up with `[2,0,1]`? Your problem would be a lot simpler if this array was indexed in the order of the new array instead of the old one. So `[1, 2, 0]` i.e. it is saying to get the first element pull our `arr[1]` etc. Then the solution is just `[arr[idx] for idx in [2, 0, 1]]`. If you explain the full problem, maybe someone can help you get this more sensible indexing in the first place? – Dan Jul 17 '20 at 17:20
  • @Georgy That is the inverse operation of what is asked for here. – alani Jul 17 '20 at 17:20
  • @Georgy Question closed while the dup answers a different question? – alani Jul 17 '20 at 17:23
  • `mylist = ["b", "c", "a"] ; myorder = [2, 0, 1] ; [mylist[i] for i in myorder]` (the answer referred to above) recovers the original list `['a', 'b', 'c']` -- i.e. the inverse operation – alani Jul 17 '20 at 17:26
  • But if the input is `mylist = ["a", "b", "c"]`, then the output is `['c', 'a', 'b']` rather than the `['b', 'c', 'a']` that is wanted here. – alani Jul 17 '20 at 17:28
  • @Sushanth This operation is not in general an array rotation. It happens to be with this particular input list of indices. – alani Jul 17 '20 at 17:37
  • @alaniwi I agree this is not a dupe. But I also think it might be an [xy-problem](http://xyproblem.info/) which is why I've asked the OP where the index array comes from. I'll bet it will end up being a dupe. But we won't know until there is clarification so as it stands your solution is spot on. – Dan Jul 17 '20 at 17:39
  • @Dan Okay, that makes sense. – alani Jul 17 '20 at 17:41
  • 2
    @alaniwi Ah! My mistake! Then the following dupe should fit here: [How to rearrange one list based on a second list of indices](https://stackoverflow.com/q/41549201/7851470) – Georgy Jul 17 '20 at 18:00
  • @ThierryLathuille Could you please update the duplicate list? – Georgy Jul 17 '20 at 18:01
  • @Georgy Yes I agree that that one is a valid dupe. – alani Jul 17 '20 at 18:10
  • Not sure if there is a way to withdraw a reopen vote? (Happy to do, contingent on the list being updated as you indicated.) – alani Jul 17 '20 at 18:13
  • @alaniwi Looks like [it's not possible yet](https://meta.stackexchange.com/q/193061/378331)... – Georgy Jul 17 '20 at 18:31
  • sorry for creating such a confusion , yes @georgy 's comment was what i was looking for – GeorgeMenezes Jul 20 '20 at 15:30

1 Answers1

2

You could do:

lst = ["a", "b", "c"]
pos = [2, 0, 1]
d = dict(zip(pos, lst))  # {2: 'a', 0: 'b', 1: 'c'}
print([d[index] for index in range(len(lst))])  # ['b', 'c', 'a']
alani
  • 12,573
  • 2
  • 13
  • 23