'How to find number of times you change a list to get from one list to another in python

I want to be able to find the number of times to change one list to another. An example is if I start out with the list [5, 1, 3, 2, 4], but want to change it to the list [4, 5, 2, 1, 3] by taking an element and moving it a number of positions to the left, where it then shifts some other elements to the right. For example, 5 1 3 2 4 Move the "4" four positions to the left to get -> 4 5 1 3 2 Then move the "2" two positions to the left to get -> 4 5 2 1 3

My attempt at the code was to find the index changes with:

n = int(input())
inp = [int(x) for x in input().split()]
out = [int(x) for x in input().split()]

ind = []
for i in range(n):
  ind.append(inp.index(i+1)-out.index(i+1))

print(ind)

But then I dont know how to proceed. i also attempted the code

def min(x, y):
    count = 0
    for i in range(len(x)):
        if x[i] != y[i]:
            count += 1
            x.insert(y.index(x[i]), x.pop(i))
    return count

but then that also didnt work because it didnt pass some of the test cases. Does anyone have a working code for this?



Solution 1:[1]

def changer_counter (l1,l2):
  count = 0
  for i in l1:
    if l1.index(i) <= l2.index(i):
      continue
    else:
      count += (l1.index(i)-l1.index(l2[l2.index(i)+1]))
  return count

print(changer_counter(l1,l2))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 PyotrVanNostrand