[英]How to find the number of shifts in a sorted list from an unsorted list in Python
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.我有一个未排序的列表lis=[4,2,1,3]
使用排序 function 我得到的值是lis=[1,2,3,4]
现在数字 1,3,4 已经移动了数到 3 班。 How do I count the number of shifts in Python?如何计算 Python 中的班次数?
You can try this.你可以试试这个。
sum(i!=j for i,j in zip(lis,sorted(lis)))
# 3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.