Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 以下算法通过实现 Fisher-Yates算法 在新列表中进行排序来将列表中的元素顺序随机打乱。
- from copy import deepcopy
- from random import randint
- def shuffle(lst):
- temp_lst = deepcopy(lst)
- m = len(temp_lst)
- while (m):
- m -= 1
- i = randint(0, m)
- temp_lst[m], temp_lst[i] = temp_lst[i], temp_lst[m]
- return temp_lst
- foo = [1,2,3]
- shuffle(foo) # [2,3,1] , foo = [1,2,3]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement