Transform one list into another [duplicate]












1















This question already has an answer here:




  • Algorithm: optimal way to rearrange a list from one order to another?

    4 answers




Given two lists, for example:



a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
b = [2, 4, 6, 7, 0, 1, 3, 5, 8, 9]


I wish to find a series of moves which will transform list a into list b, where each move is an operation:



move(from_index, to_index)


which moves the element at location from_index and places it at location to_index. So if:



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


then the operation move(3,1) on the list a will transform a into:



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









share|improve this question













marked as duplicate by squeamish ossifrage, Community Nov 23 '18 at 10:54


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • Is the transformation always possible?
    – merlyn
    Nov 23 '18 at 7:43










  • is this Python?
    – snoram
    Nov 23 '18 at 7:44










  • Are these lists permutations of each other?
    – MBo
    Nov 23 '18 at 7:48










  • Are you looking for something more specific like the minimum number of moves?
    – Mark Meyer
    Nov 23 '18 at 7:52






  • 1




    So basically you want insertion sort
    – Paul
    Nov 23 '18 at 7:58
















1















This question already has an answer here:




  • Algorithm: optimal way to rearrange a list from one order to another?

    4 answers




Given two lists, for example:



a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
b = [2, 4, 6, 7, 0, 1, 3, 5, 8, 9]


I wish to find a series of moves which will transform list a into list b, where each move is an operation:



move(from_index, to_index)


which moves the element at location from_index and places it at location to_index. So if:



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


then the operation move(3,1) on the list a will transform a into:



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









share|improve this question













marked as duplicate by squeamish ossifrage, Community Nov 23 '18 at 10:54


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • Is the transformation always possible?
    – merlyn
    Nov 23 '18 at 7:43










  • is this Python?
    – snoram
    Nov 23 '18 at 7:44










  • Are these lists permutations of each other?
    – MBo
    Nov 23 '18 at 7:48










  • Are you looking for something more specific like the minimum number of moves?
    – Mark Meyer
    Nov 23 '18 at 7:52






  • 1




    So basically you want insertion sort
    – Paul
    Nov 23 '18 at 7:58














1












1








1








This question already has an answer here:




  • Algorithm: optimal way to rearrange a list from one order to another?

    4 answers




Given two lists, for example:



a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
b = [2, 4, 6, 7, 0, 1, 3, 5, 8, 9]


I wish to find a series of moves which will transform list a into list b, where each move is an operation:



move(from_index, to_index)


which moves the element at location from_index and places it at location to_index. So if:



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


then the operation move(3,1) on the list a will transform a into:



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









share|improve this question














This question already has an answer here:




  • Algorithm: optimal way to rearrange a list from one order to another?

    4 answers




Given two lists, for example:



a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
b = [2, 4, 6, 7, 0, 1, 3, 5, 8, 9]


I wish to find a series of moves which will transform list a into list b, where each move is an operation:



move(from_index, to_index)


which moves the element at location from_index and places it at location to_index. So if:



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


then the operation move(3,1) on the list a will transform a into:



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




This question already has an answer here:




  • Algorithm: optimal way to rearrange a list from one order to another?

    4 answers








algorithm






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 7:33









mkam

258110




258110




marked as duplicate by squeamish ossifrage, Community Nov 23 '18 at 10:54


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by squeamish ossifrage, Community Nov 23 '18 at 10:54


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • Is the transformation always possible?
    – merlyn
    Nov 23 '18 at 7:43










  • is this Python?
    – snoram
    Nov 23 '18 at 7:44










  • Are these lists permutations of each other?
    – MBo
    Nov 23 '18 at 7:48










  • Are you looking for something more specific like the minimum number of moves?
    – Mark Meyer
    Nov 23 '18 at 7:52






  • 1




    So basically you want insertion sort
    – Paul
    Nov 23 '18 at 7:58


















  • Is the transformation always possible?
    – merlyn
    Nov 23 '18 at 7:43










  • is this Python?
    – snoram
    Nov 23 '18 at 7:44










  • Are these lists permutations of each other?
    – MBo
    Nov 23 '18 at 7:48










  • Are you looking for something more specific like the minimum number of moves?
    – Mark Meyer
    Nov 23 '18 at 7:52






  • 1




    So basically you want insertion sort
    – Paul
    Nov 23 '18 at 7:58
















Is the transformation always possible?
– merlyn
Nov 23 '18 at 7:43




Is the transformation always possible?
– merlyn
Nov 23 '18 at 7:43












is this Python?
– snoram
Nov 23 '18 at 7:44




is this Python?
– snoram
Nov 23 '18 at 7:44












Are these lists permutations of each other?
– MBo
Nov 23 '18 at 7:48




Are these lists permutations of each other?
– MBo
Nov 23 '18 at 7:48












Are you looking for something more specific like the minimum number of moves?
– Mark Meyer
Nov 23 '18 at 7:52




Are you looking for something more specific like the minimum number of moves?
– Mark Meyer
Nov 23 '18 at 7:52




1




1




So basically you want insertion sort
– Paul
Nov 23 '18 at 7:58




So basically you want insertion sort
– Paul
Nov 23 '18 at 7:58












2 Answers
2






active

oldest

votes


















1














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

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



move(0, 8)

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



move(0, 8)

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



move(1, 8)

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



move(2, 8)

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



a==b



Hopefully that's what you're looking for.



Basically, start with the left- most element and move it to where it should be. For example, I took 0 and placed it right after the value that it is supposed to eventually end up behind, which is 7. I continued moving from left to right until all of the elements were in the desired order.






share|improve this answer



















  • 2




    I think the OP is looking for a general algorithm, not the specific moves to this one case.
    – Mark Meyer
    Nov 23 '18 at 7:51










  • @MarkMeyer good point. I just added a summary of my general strategy to the end of my answer.
    – Snake14
    Nov 23 '18 at 7:53










  • Thanks for this solution. It works well. Can you optimize it so it is optimal or nearly optimal? For example, this algorithm would take 5 moves to transform [0,1,2,3,4,5] -> [5,0,1,2,3,4], when one move is sufficient (move(5,0)).
    – mkam
    Nov 23 '18 at 8:57





















1














I'd iterate over the second sequence (the sorted list) and swap items in the first. I wrote this pseudo-code in python:



>>> a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> b = [2, 4, 6, 7, 0, 1, 3, 5, 8, 9]
>>> def swap(seq, i, j):
... a = seq[i]
... seq[i] = seq[j]
... seq[j] = a
...
>>> for index_in_b, value in enumerate(b):
... index_in_a = a.index(value)
... if index_in_b != index_in_a:
... swap(a, index_in_a, index_in_b)
... print('move {} to {}'.format(index_in_a, index_in_b))

move 0 to 2
move 1 to 4
move 2 to 6
move 3 to 7
move 4 to 6
move 5 to 6
move 6 to 7


In this case I'm moving the items in the first sequence by swapping them.



Update



We can slightly improve the performance in python by removing the move inside swap function and also removing the function call. Here is a performance comparison:



import timeit
s1 = """
a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
b = [2, 4, 6, 7, 0, 1, 3, 5, 8, 9]
def swap(seq, i, j):
a = seq[i]
seq[i] = seq[j]
seq[j] = a

for index_in_b, value in enumerate(b):
index_in_a = a.index(value)
if index_in_b != index_in_a:
swap(a, index_in_a, index_in_b)"""

s2 = """
a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
b = [2, 4, 6, 7, 0, 1, 3, 5, 8, 9]
for index_in_b, value in enumerate(b):
index_in_a = a.index(value)
if index_in_b != index_in_a:
a[index_in_a], a[index_in_b] = a[index_in_b], a[index_in_a]"""


# on an i7 macbook pro
timeit.timeit(s1)
4.087386846542358
timeit.timeit(s2)
3.5381240844726562


Slightly better, but for sure there are better ways to achieve this.






share|improve this answer























  • Thanks for your solution, although converting a swap operation into two move operations is going to be too expensive.
    – mkam
    Nov 23 '18 at 8:53










  • We can make it slightly faster by moving both at one go. I'll update the answer with timeit results.
    – Mehdi Sadeghi
    Nov 23 '18 at 9:17


















2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














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

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



move(0, 8)

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



move(0, 8)

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



move(1, 8)

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



move(2, 8)

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



a==b



Hopefully that's what you're looking for.



Basically, start with the left- most element and move it to where it should be. For example, I took 0 and placed it right after the value that it is supposed to eventually end up behind, which is 7. I continued moving from left to right until all of the elements were in the desired order.






share|improve this answer



















  • 2




    I think the OP is looking for a general algorithm, not the specific moves to this one case.
    – Mark Meyer
    Nov 23 '18 at 7:51










  • @MarkMeyer good point. I just added a summary of my general strategy to the end of my answer.
    – Snake14
    Nov 23 '18 at 7:53










  • Thanks for this solution. It works well. Can you optimize it so it is optimal or nearly optimal? For example, this algorithm would take 5 moves to transform [0,1,2,3,4,5] -> [5,0,1,2,3,4], when one move is sufficient (move(5,0)).
    – mkam
    Nov 23 '18 at 8:57


















1














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

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



move(0, 8)

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



move(0, 8)

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



move(1, 8)

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



move(2, 8)

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



a==b



Hopefully that's what you're looking for.



Basically, start with the left- most element and move it to where it should be. For example, I took 0 and placed it right after the value that it is supposed to eventually end up behind, which is 7. I continued moving from left to right until all of the elements were in the desired order.






share|improve this answer



















  • 2




    I think the OP is looking for a general algorithm, not the specific moves to this one case.
    – Mark Meyer
    Nov 23 '18 at 7:51










  • @MarkMeyer good point. I just added a summary of my general strategy to the end of my answer.
    – Snake14
    Nov 23 '18 at 7:53










  • Thanks for this solution. It works well. Can you optimize it so it is optimal or nearly optimal? For example, this algorithm would take 5 moves to transform [0,1,2,3,4,5] -> [5,0,1,2,3,4], when one move is sufficient (move(5,0)).
    – mkam
    Nov 23 '18 at 8:57
















1












1








1






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

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



move(0, 8)

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



move(0, 8)

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



move(1, 8)

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



move(2, 8)

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



a==b



Hopefully that's what you're looking for.



Basically, start with the left- most element and move it to where it should be. For example, I took 0 and placed it right after the value that it is supposed to eventually end up behind, which is 7. I continued moving from left to right until all of the elements were in the desired order.






share|improve this answer














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

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



move(0, 8)

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



move(0, 8)

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



move(1, 8)

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



move(2, 8)

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



a==b



Hopefully that's what you're looking for.



Basically, start with the left- most element and move it to where it should be. For example, I took 0 and placed it right after the value that it is supposed to eventually end up behind, which is 7. I continued moving from left to right until all of the elements were in the desired order.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 23 '18 at 8:06

























answered Nov 23 '18 at 7:50









Snake14

28917




28917








  • 2




    I think the OP is looking for a general algorithm, not the specific moves to this one case.
    – Mark Meyer
    Nov 23 '18 at 7:51










  • @MarkMeyer good point. I just added a summary of my general strategy to the end of my answer.
    – Snake14
    Nov 23 '18 at 7:53










  • Thanks for this solution. It works well. Can you optimize it so it is optimal or nearly optimal? For example, this algorithm would take 5 moves to transform [0,1,2,3,4,5] -> [5,0,1,2,3,4], when one move is sufficient (move(5,0)).
    – mkam
    Nov 23 '18 at 8:57
















  • 2




    I think the OP is looking for a general algorithm, not the specific moves to this one case.
    – Mark Meyer
    Nov 23 '18 at 7:51










  • @MarkMeyer good point. I just added a summary of my general strategy to the end of my answer.
    – Snake14
    Nov 23 '18 at 7:53










  • Thanks for this solution. It works well. Can you optimize it so it is optimal or nearly optimal? For example, this algorithm would take 5 moves to transform [0,1,2,3,4,5] -> [5,0,1,2,3,4], when one move is sufficient (move(5,0)).
    – mkam
    Nov 23 '18 at 8:57










2




2




I think the OP is looking for a general algorithm, not the specific moves to this one case.
– Mark Meyer
Nov 23 '18 at 7:51




I think the OP is looking for a general algorithm, not the specific moves to this one case.
– Mark Meyer
Nov 23 '18 at 7:51












@MarkMeyer good point. I just added a summary of my general strategy to the end of my answer.
– Snake14
Nov 23 '18 at 7:53




@MarkMeyer good point. I just added a summary of my general strategy to the end of my answer.
– Snake14
Nov 23 '18 at 7:53












Thanks for this solution. It works well. Can you optimize it so it is optimal or nearly optimal? For example, this algorithm would take 5 moves to transform [0,1,2,3,4,5] -> [5,0,1,2,3,4], when one move is sufficient (move(5,0)).
– mkam
Nov 23 '18 at 8:57






Thanks for this solution. It works well. Can you optimize it so it is optimal or nearly optimal? For example, this algorithm would take 5 moves to transform [0,1,2,3,4,5] -> [5,0,1,2,3,4], when one move is sufficient (move(5,0)).
– mkam
Nov 23 '18 at 8:57















1














I'd iterate over the second sequence (the sorted list) and swap items in the first. I wrote this pseudo-code in python:



>>> a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> b = [2, 4, 6, 7, 0, 1, 3, 5, 8, 9]
>>> def swap(seq, i, j):
... a = seq[i]
... seq[i] = seq[j]
... seq[j] = a
...
>>> for index_in_b, value in enumerate(b):
... index_in_a = a.index(value)
... if index_in_b != index_in_a:
... swap(a, index_in_a, index_in_b)
... print('move {} to {}'.format(index_in_a, index_in_b))

move 0 to 2
move 1 to 4
move 2 to 6
move 3 to 7
move 4 to 6
move 5 to 6
move 6 to 7


In this case I'm moving the items in the first sequence by swapping them.



Update



We can slightly improve the performance in python by removing the move inside swap function and also removing the function call. Here is a performance comparison:



import timeit
s1 = """
a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
b = [2, 4, 6, 7, 0, 1, 3, 5, 8, 9]
def swap(seq, i, j):
a = seq[i]
seq[i] = seq[j]
seq[j] = a

for index_in_b, value in enumerate(b):
index_in_a = a.index(value)
if index_in_b != index_in_a:
swap(a, index_in_a, index_in_b)"""

s2 = """
a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
b = [2, 4, 6, 7, 0, 1, 3, 5, 8, 9]
for index_in_b, value in enumerate(b):
index_in_a = a.index(value)
if index_in_b != index_in_a:
a[index_in_a], a[index_in_b] = a[index_in_b], a[index_in_a]"""


# on an i7 macbook pro
timeit.timeit(s1)
4.087386846542358
timeit.timeit(s2)
3.5381240844726562


Slightly better, but for sure there are better ways to achieve this.






share|improve this answer























  • Thanks for your solution, although converting a swap operation into two move operations is going to be too expensive.
    – mkam
    Nov 23 '18 at 8:53










  • We can make it slightly faster by moving both at one go. I'll update the answer with timeit results.
    – Mehdi Sadeghi
    Nov 23 '18 at 9:17
















1














I'd iterate over the second sequence (the sorted list) and swap items in the first. I wrote this pseudo-code in python:



>>> a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> b = [2, 4, 6, 7, 0, 1, 3, 5, 8, 9]
>>> def swap(seq, i, j):
... a = seq[i]
... seq[i] = seq[j]
... seq[j] = a
...
>>> for index_in_b, value in enumerate(b):
... index_in_a = a.index(value)
... if index_in_b != index_in_a:
... swap(a, index_in_a, index_in_b)
... print('move {} to {}'.format(index_in_a, index_in_b))

move 0 to 2
move 1 to 4
move 2 to 6
move 3 to 7
move 4 to 6
move 5 to 6
move 6 to 7


In this case I'm moving the items in the first sequence by swapping them.



Update



We can slightly improve the performance in python by removing the move inside swap function and also removing the function call. Here is a performance comparison:



import timeit
s1 = """
a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
b = [2, 4, 6, 7, 0, 1, 3, 5, 8, 9]
def swap(seq, i, j):
a = seq[i]
seq[i] = seq[j]
seq[j] = a

for index_in_b, value in enumerate(b):
index_in_a = a.index(value)
if index_in_b != index_in_a:
swap(a, index_in_a, index_in_b)"""

s2 = """
a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
b = [2, 4, 6, 7, 0, 1, 3, 5, 8, 9]
for index_in_b, value in enumerate(b):
index_in_a = a.index(value)
if index_in_b != index_in_a:
a[index_in_a], a[index_in_b] = a[index_in_b], a[index_in_a]"""


# on an i7 macbook pro
timeit.timeit(s1)
4.087386846542358
timeit.timeit(s2)
3.5381240844726562


Slightly better, but for sure there are better ways to achieve this.






share|improve this answer























  • Thanks for your solution, although converting a swap operation into two move operations is going to be too expensive.
    – mkam
    Nov 23 '18 at 8:53










  • We can make it slightly faster by moving both at one go. I'll update the answer with timeit results.
    – Mehdi Sadeghi
    Nov 23 '18 at 9:17














1












1








1






I'd iterate over the second sequence (the sorted list) and swap items in the first. I wrote this pseudo-code in python:



>>> a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> b = [2, 4, 6, 7, 0, 1, 3, 5, 8, 9]
>>> def swap(seq, i, j):
... a = seq[i]
... seq[i] = seq[j]
... seq[j] = a
...
>>> for index_in_b, value in enumerate(b):
... index_in_a = a.index(value)
... if index_in_b != index_in_a:
... swap(a, index_in_a, index_in_b)
... print('move {} to {}'.format(index_in_a, index_in_b))

move 0 to 2
move 1 to 4
move 2 to 6
move 3 to 7
move 4 to 6
move 5 to 6
move 6 to 7


In this case I'm moving the items in the first sequence by swapping them.



Update



We can slightly improve the performance in python by removing the move inside swap function and also removing the function call. Here is a performance comparison:



import timeit
s1 = """
a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
b = [2, 4, 6, 7, 0, 1, 3, 5, 8, 9]
def swap(seq, i, j):
a = seq[i]
seq[i] = seq[j]
seq[j] = a

for index_in_b, value in enumerate(b):
index_in_a = a.index(value)
if index_in_b != index_in_a:
swap(a, index_in_a, index_in_b)"""

s2 = """
a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
b = [2, 4, 6, 7, 0, 1, 3, 5, 8, 9]
for index_in_b, value in enumerate(b):
index_in_a = a.index(value)
if index_in_b != index_in_a:
a[index_in_a], a[index_in_b] = a[index_in_b], a[index_in_a]"""


# on an i7 macbook pro
timeit.timeit(s1)
4.087386846542358
timeit.timeit(s2)
3.5381240844726562


Slightly better, but for sure there are better ways to achieve this.






share|improve this answer














I'd iterate over the second sequence (the sorted list) and swap items in the first. I wrote this pseudo-code in python:



>>> a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> b = [2, 4, 6, 7, 0, 1, 3, 5, 8, 9]
>>> def swap(seq, i, j):
... a = seq[i]
... seq[i] = seq[j]
... seq[j] = a
...
>>> for index_in_b, value in enumerate(b):
... index_in_a = a.index(value)
... if index_in_b != index_in_a:
... swap(a, index_in_a, index_in_b)
... print('move {} to {}'.format(index_in_a, index_in_b))

move 0 to 2
move 1 to 4
move 2 to 6
move 3 to 7
move 4 to 6
move 5 to 6
move 6 to 7


In this case I'm moving the items in the first sequence by swapping them.



Update



We can slightly improve the performance in python by removing the move inside swap function and also removing the function call. Here is a performance comparison:



import timeit
s1 = """
a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
b = [2, 4, 6, 7, 0, 1, 3, 5, 8, 9]
def swap(seq, i, j):
a = seq[i]
seq[i] = seq[j]
seq[j] = a

for index_in_b, value in enumerate(b):
index_in_a = a.index(value)
if index_in_b != index_in_a:
swap(a, index_in_a, index_in_b)"""

s2 = """
a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
b = [2, 4, 6, 7, 0, 1, 3, 5, 8, 9]
for index_in_b, value in enumerate(b):
index_in_a = a.index(value)
if index_in_b != index_in_a:
a[index_in_a], a[index_in_b] = a[index_in_b], a[index_in_a]"""


# on an i7 macbook pro
timeit.timeit(s1)
4.087386846542358
timeit.timeit(s2)
3.5381240844726562


Slightly better, but for sure there are better ways to achieve this.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 23 '18 at 9:28

























answered Nov 23 '18 at 8:27









Mehdi Sadeghi

2,6951725




2,6951725












  • Thanks for your solution, although converting a swap operation into two move operations is going to be too expensive.
    – mkam
    Nov 23 '18 at 8:53










  • We can make it slightly faster by moving both at one go. I'll update the answer with timeit results.
    – Mehdi Sadeghi
    Nov 23 '18 at 9:17


















  • Thanks for your solution, although converting a swap operation into two move operations is going to be too expensive.
    – mkam
    Nov 23 '18 at 8:53










  • We can make it slightly faster by moving both at one go. I'll update the answer with timeit results.
    – Mehdi Sadeghi
    Nov 23 '18 at 9:17
















Thanks for your solution, although converting a swap operation into two move operations is going to be too expensive.
– mkam
Nov 23 '18 at 8:53




Thanks for your solution, although converting a swap operation into two move operations is going to be too expensive.
– mkam
Nov 23 '18 at 8:53












We can make it slightly faster by moving both at one go. I'll update the answer with timeit results.
– Mehdi Sadeghi
Nov 23 '18 at 9:17




We can make it slightly faster by moving both at one go. I'll update the answer with timeit results.
– Mehdi Sadeghi
Nov 23 '18 at 9:17



Popular posts from this blog

What visual should I use to simply compare current year value vs last year in Power BI desktop

Alexandru Averescu

Trompette piccolo