+ 3

Python List Query !!!!!!!!

If two integer lists are given in python the second list is sorted list of first list then how to find that how many elements are replaced from there places (OR how many elements are not at there place or index after the sorting of list) Note : I need a algorithm which do this problem without traversing the whole lists

19th Sep 2018, 2:26 PM
Rakesh Patil
Rakesh Patil - avatar
1 ответ
+ 5
import random l1 = [1,2,3,4,5] l2 = random.sample(l1, len(l1)) print('Sorted: ', l1, '\nUnsorted:', l2) count = 0 for sorted, unsorted in zip(l1, l2): if sorted != unsorted: count += 1 print(count)
19th Sep 2018, 3:01 PM
Mert Yazıcı
Mert Yazıcı - avatar