0

I have an unsorted list lis=[4,2,1,3] using the sort function I get the value as lis=[1,2,3,4] now the numbers 1,3,4 have been shifted making the shift count to 3 shifts. How do I count the number of shifts in Python?

Ch3steR
  • 20,090
  • 4
  • 28
  • 58
Glen Veigas
  • 186
  • 11

1 Answers1

1

You can try this.

sum(i!=j for i,j in zip(lis,sorted(lis)))
# 3
Ch3steR
  • 20,090
  • 4
  • 28
  • 58