Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.42 KB | None | 0 0
  1. # 以下算法通过实现 Fisher-Yates算法 在新列表中进行排序来将列表中的元素顺序随机打乱。
  2.  
  3. from copy import deepcopy
  4. from random import randint
  5. def shuffle(lst):
  6. temp_lst = deepcopy(lst)
  7. m = len(temp_lst)
  8. while (m):
  9. m -= 1
  10. i = randint(0, m)
  11. temp_lst[m], temp_lst[i] = temp_lst[i], temp_lst[m]
  12. return temp_lst
  13.  
  14. foo = [1,2,3]
  15. shuffle(foo) # [2,3,1] , foo = [1,2,3]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement