Phương pháp ngẫu nhiên
Thí dụ
Shuffle một danh sách [tổ chức lại thứ tự của các mục danh sách]:
Nhập ngẫu nhiên
mylist = ["Apple", "chuối", "anh đào"] ngẫu nhiên.shuffle [mylist]
random.shuffle[mylist]
print[mylist]
Hãy tự mình thử »
Định nghĩa và cách sử dụng
Phương thức shuffle[]
có một chuỗi, như một danh sách và tổ chức lại thứ tự của các mục.
Lưu ý: Phương pháp này thay đổi danh sách ban đầu, nó không trả về một danh sách mới. This method changes the original list, it does not return a new list.
Cú pháp
ngẫu nhiên.shuffle [trình tự, chức năng]
Giá trị tham số
sự phối hợp | Yêu cầu. Một chu kỳ. |
hàm số | Không bắt buộc. Tên của một hàm trả về một số từ 0,0 đến 1.0. Nếu không được chỉ định, hàm
random[] sẽ được sử dụngIf not specified, the function
random[] will be used
|
Nhiều ví dụ hơn
Thí dụ
Bạn có thể xác định chức năng của riêng bạn để cân hoặc chỉ định kết quả.
Nếu hàm trả về cùng một số mỗi lần, kết quả sẽ theo cùng một thứ tự mỗi lần:
Nhập ngẫu nhiên
mylist = ["Apple", "chuối", "anh đào"] ngẫu nhiên.shuffle [mylist]
return 0.1
Hãy tự mình thử »
random.shuffle[mylist, myfunction]
print[mylist]
Hãy tự mình thử »
Phương pháp ngẫu nhiên
Trong một số trường hợp khi sử dụng các mảng numpy, sử dụng random.shuffle
đã tạo dữ liệu trùng lặp trong mảng.
Một giải pháp thay thế là sử dụng
>>> foo = np.array[[[1,2,3],[4,5,6],[7,8,9]]]
>>> foo
array[[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]]
>>> random.shuffle[foo]
>>> foo
array[[[1, 2, 3],
[1, 2, 3],
[4, 5, 6]]]
0. Nếu bạn đã làm việc với Numpy, đây là phương pháp ưa thích so với random.shuffle
chung.numpy.random.shuffle
Thí dụ
>>> import numpy as np
>>> import random
Sử dụng random.shuffle
:
>>> foo = np.array[[[1,2,3],[4,5,6],[7,8,9]]]
>>> foo
array[[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]]
>>> random.shuffle[foo]
>>> foo
array[[[1, 2, 3],
[1, 2, 3],
[4, 5, 6]]]
Sử dụng
>>> foo = np.array[[[1,2,3],[4,5,6],[7,8,9]]]
>>> foo
array[[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]]
>>> random.shuffle[foo]
>>> foo
array[[[1, 2, 3],
[1, 2, 3],
[4, 5, 6]]]
0:>>> foo = np.array[[[1,2,3],[4,5,6],[7,8,9]]]
>>> foo
array[[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]]
>>> np.random.shuffle[foo]
>>> foo
array[[[1, 2, 3],
[7, 8, 9],
[4, 5, 6]]]
Xáo trộn một chuỗi các con số luôn là một tiện ích hữu ích và câu hỏi đã xuất hiện trong nhiều cuộc phỏng vấn vị trí của công ty. Biết nhiều hơn một phương pháp để đạt được điều này luôn có thể là một điểm cộng. Hãy để thảo luận về một số cách nhất định trong đó điều này có thể đạt được.Method #1: FisherTHER Yates Shuffle Thuật toán này là một trong những thuật toán nổi tiếng chủ yếu được sử dụng để xáo trộn một chuỗi số trong Python. Thuật toán này chỉ lấy giá trị chỉ số cao hơn và hoán đổi nó với giá trị hiện tại, quá trình này lặp lại trong một vòng lặp cho đến cuối danh sách. & NBSP; & nbsp;
Method #1 : Fisher–Yates shuffle Algorithm
This is one of
the famous algorithms that is mainly employed to shuffle a sequence of numbers in python. This algorithm just takes the higher index value, and swaps it with current value, this process repeats in a loop till end of the list.
Python3
>>> foo = np.array[[[1,2,3],[4,5,6],[7,8,9]]]
>>> foo
array[[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]]
>>> random.shuffle[foo]
>>> foo
array[[[1, 2, 3],
[1, 2, 3],
[4, 5, 6]]]
4 >>> foo = np.array[[[1,2,3],[4,5,6],[7,8,9]]]
>>> foo
array[[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]]
>>> random.shuffle[foo]
>>> foo
array[[[1, 2, 3],
[1, 2, 3],
[4, 5, 6]]]
5>>> foo = np.array[[[1,2,3],[4,5,6],[7,8,9]]]
>>> foo
array[[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]]
>>> random.shuffle[foo]
>>> foo
array[[[1, 2, 3],
[1, 2, 3],
[4, 5, 6]]]
6>>> foo = np.array[[[1,2,3],[4,5,6],[7,8,9]]]
>>> foo
array[[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]]
>>> random.shuffle[foo]
>>> foo
array[[[1, 2, 3],
[1, 2, 3],
[4, 5, 6]]]
7 >>> foo = np.array[[[1,2,3],[4,5,6],[7,8,9]]]
>>> foo
array[[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]]
>>> random.shuffle[foo]
>>> foo
array[[[1, 2, 3],
[1, 2, 3],
[4, 5, 6]]]
8>>> foo = np.array[[[1,2,3],[4,5,6],[7,8,9]]]
>>> foo
array[[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]]
>>> random.shuffle[foo]
>>> foo
array[[[1, 2, 3],
[1, 2, 3],
[4, 5, 6]]]
9>>> foo = np.array[[[1,2,3],[4,5,6],[7,8,9]]]
>>> foo
array[[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]]
>>> np.random.shuffle[foo]
>>> foo
array[[[1, 2, 3],
[7, 8, 9],
[4, 5, 6]]]
0>>> foo = np.array[[[1,2,3],[4,5,6],[7,8,9]]]
>>> foo
array[[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]]
>>> np.random.shuffle[foo]
>>> foo
array[[[1, 2, 3],
[7, 8, 9],
[4, 5, 6]]]
1>>> foo = np.array[[[1,2,3],[4,5,6],[7,8,9]]]
>>> foo
array[[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]]
>>> np.random.shuffle[foo]
>>> foo
array[[[1, 2, 3],
[7, 8, 9],
[4, 5, 6]]]
0>>> foo = np.array[[[1,2,3],[4,5,6],[7,8,9]]]
>>> foo
array[[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]]
>>> np.random.shuffle[foo]
>>> foo
array[[[1, 2, 3],
[7, 8, 9],
[4, 5, 6]]]
3______>>> foo = np.array[[[1,2,3],[4,5,6],[7,8,9]]]
>>> foo
array[[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]]
>>> np.random.shuffle[foo]
>>> foo
array[[[1, 2, 3],
[7, 8, 9],
[4, 5, 6]]]
9 The original list is : [1, 4, 5, 6, 3] The shuffled list is : [4, 3, 1, 5, 6]0
The original list is : [1, 4, 5, 6, 3] The shuffled list is : [4, 3, 1, 5, 6]1
The original list is : [1, 4, 5, 6, 3] The shuffled list is : [4, 3, 1, 5, 6]2
The original list is : [1, 4, 5, 6, 3] The shuffled list is : [4, 3, 1, 5, 6]3
The original list is : [1, 4, 5, 6, 3] The shuffled list is : [4, 3, 1, 5, 6]4
Is
The original list is : [1, 4, 5, 6, 3] The shuffled list is : [5, 3, 6, 1, 4]0
The original list is : [1, 4, 5, 6, 3] The shuffled list is : [5, 3, 6, 1, 4]1
>>> foo = np.array[[[1,2,3],[4,5,6],[7,8,9]]]
>>> foo
array[[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]]
>>> random.shuffle[foo]
>>> foo
array[[[1, 2, 3],
[1, 2, 3],
[4, 5, 6]]]
7 The original list is : [1, 4, 5, 6, 3] The shuffled list is : [5, 3, 6, 1, 4]3
The original list is : [1, 4, 5, 6, 3] The shuffled list is : [5, 6, 4, 3, 1]5__5555532
The original list is : [1, 4, 5, 6, 3] The shuffled list is : [5, 3, 6, 1, 4]0
Original List: [1, 2, 3, 4, 5, 6] Shuffled List: [4, 5, 3, 2, 6, 1]0
>>> foo = np.array[[[1,2,3],[4,5,6],[7,8,9]]]
>>> foo
array[[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]]
>>> random.shuffle[foo]
>>> foo
array[[[1, 2, 3],
[1, 2, 3],
[4, 5, 6]]]
7 Original List: [1, 2, 3, 4, 5, 6] Shuffled List: [4, 5, 3, 2, 6, 1]2
>>> foo = np.array[[[1,2,3],[4,5,6],[7,8,9]]]
>>> foo
array[[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]]
>>> np.random.shuffle[foo]
>>> foo
array[[[1, 2, 3],
[7, 8, 9],
[4, 5, 6]]]
9 The original list is : [1, 4, 5, 6, 3] The shuffled list is : [4, 3, 1, 5, 6]0
Original List: [1, 2, 3, 4, 5, 6] Shuffled List: [4, 5, 3, 2, 6, 1]5 ________ 32 & nbsp;
The original list is : [1, 4, 5, 6, 3] The shuffled list is : [4, 3, 1, 5, 6]3
The original list is : [1, 4, 5, 6, 3] The shuffled list is : [4, 3, 1, 5, 6]4
Output:
The original list is : [1, 4, 5, 6, 3] The shuffled list is : [4, 3, 1, 5, 6]
& nbsp; & nbsp; Phương thức #2: sử dụng Random.shuffle [] & nbsp; Đây là phương thức được khuyến nghị nhất để xáo trộn danh sách. Python trong thư viện ngẫu nhiên của nó cung cấp chức năng sẵn có này mà tại chỗ xáo trộn danh sách. Hạn chế của điều này là đặt hàng danh sách bị mất trong quá trình này. Hữu ích cho các nhà phát triển chọn tiết kiệm thời gian và hối hả. & Nbsp;
Method #2 : Using random.shuffle[]
This is most recommended method to shuffle a list. Python
in its random library provides this inbuilt function which in-place shuffles the list. Drawback of this is that list ordering is lost in this process. Useful for developers who choose to save time and hustle.
Python3
>>> foo = np.array[[[1,2,3],[4,5,6],[7,8,9]]]
>>> foo
array[[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]]
>>> random.shuffle[foo]
>>> foo
array[[[1, 2, 3],
[1, 2, 3],
[4, 5, 6]]]
4 >>> foo = np.array[[[1,2,3],[4,5,6],[7,8,9]]]
>>> foo
array[[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]]
>>> random.shuffle[foo]
>>> foo
array[[[1, 2, 3],
[1, 2, 3],
[4, 5, 6]]]
5>>> foo = np.array[[[1,2,3],[4,5,6],[7,8,9]]]
>>> foo
array[[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]]
>>> random.shuffle[foo]
>>> foo
array[[[1, 2, 3],
[1, 2, 3],
[4, 5, 6]]]
6>>> foo = np.array[[[1,2,3],[4,5,6],[7,8,9]]]
>>> foo
array[[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]]
>>> random.shuffle[foo]
>>> foo
array[[[1, 2, 3],
[1, 2, 3],
[4, 5, 6]]]
7 >>> foo = np.array[[[1,2,3],[4,5,6],[7,8,9]]]
>>> foo
array[[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]]
>>> random.shuffle[foo]
>>> foo
array[[[1, 2, 3],
[1, 2, 3],
[4, 5, 6]]]
8>>> foo = np.array[[[1,2,3],[4,5,6],[7,8,9]]]
>>> foo
array[[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]]
>>> random.shuffle[foo]
>>> foo
array[[[1, 2, 3],
[1, 2, 3],
[4, 5, 6]]]
9>>> foo = np.array[[[1,2,3],[4,5,6],[7,8,9]]]
>>> foo
array[[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]]
>>> np.random.shuffle[foo]
>>> foo
array[[[1, 2, 3],
[7, 8, 9],
[4, 5, 6]]]
0>>> foo = np.array[[[1,2,3],[4,5,6],[7,8,9]]]
>>> foo
array[[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]]
>>> np.random.shuffle[foo]
>>> foo
array[[[1, 2, 3],
[7, 8, 9],
[4, 5, 6]]]
1>>> foo = np.array[[[1,2,3],[4,5,6],[7,8,9]]]
>>> foo
array[[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]]
>>> np.random.shuffle[foo]
>>> foo
array[[[1, 2, 3],
[7, 8, 9],
[4, 5, 6]]]
0>>> foo = np.array[[[1,2,3],[4,5,6],[7,8,9]]]
>>> foo
array[[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]]
>>> np.random.shuffle[foo]
>>> foo
array[[[1, 2, 3],
[7, 8, 9],
[4, 5, 6]]]
3______>>> foo = np.array[[[1,2,3],[4,5,6],[7,8,9]]]
>>> foo
array[[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]]
>>> np.random.shuffle[foo]
>>> foo
array[[[1, 2, 3],
[7, 8, 9],
[4, 5, 6]]]
9 The original list is : [1, 4, 5, 6, 3] The shuffled list is : [4, 3, 1, 5, 6]0
The original list is : [1, 4, 5, 6, 3] The shuffled list is : [4, 3, 1, 5, 6]1
The original list is : [1, 4, 5, 6, 3] The shuffled list is : [4, 3, 1, 5, 6]2
The original list is : [1, 4, 5, 6, 3] The shuffled list is : [4, 3, 1, 5, 6]3
The original list is : [1, 4, 5, 6, 3] The shuffled list is : [4, 3, 1, 5, 6]4
random.shuffle
0
>>> foo = np.array[[[1,2,3],[4,5,6],[7,8,9]]]
>>> foo
array[[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]]
>>> np.random.shuffle[foo]
>>> foo
array[[[1, 2, 3],
[7, 8, 9],
[4, 5, 6]]]
9 The original list is : [1, 4, 5, 6, 3] The shuffled list is : [4, 3, 1, 5, 6]0
Original List: [1, 2, 3, 4, 5, 6] Shuffled List: [4, 5, 3, 2, 6, 1]5 ________ 32 & nbsp;
The original list is : [1, 4, 5, 6, 3] The shuffled list is : [4, 3, 1, 5, 6]3
The original list is : [1, 4, 5, 6, 3] The shuffled list is : [4, 3, 1, 5, 6]4
Output:
The original list is : [1, 4, 5, 6, 3] The shuffled list is : [5, 6, 4, 3, 1]
& nbsp; & nbsp; Phương thức #2: sử dụng Random.shuffle [] & nbsp; Đây là phương thức được khuyến nghị nhất để xáo trộn danh sách. Python trong thư viện ngẫu nhiên của nó cung cấp chức năng sẵn có này mà tại chỗ xáo trộn danh sách. Hạn chế của điều này là đặt hàng danh sách bị mất trong quá trình này. Hữu ích cho các nhà phát triển chọn tiết kiệm thời gian và hối hả. & Nbsp;
Method #3 : Using random.sample[]
This is quite a useful function, better than the shuffle method used above in aspect that it creates a new shuffled list and returns it rather than disturbing the order of original list. This is useful in cases we require to retain the original list.
Python3
>>> foo = np.array[[[1,2,3],[4,5,6],[7,8,9]]]
>>> foo
array[[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]]
>>> random.shuffle[foo]
>>> foo
array[[[1, 2, 3],
[1, 2, 3],
[4, 5, 6]]]
4 >>> foo = np.array[[[1,2,3],[4,5,6],[7,8,9]]]
>>> foo
array[[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]]
>>> random.shuffle[foo]
>>> foo
array[[[1, 2, 3],
[1, 2, 3],
[4, 5, 6]]]
5>>> foo = np.array[[[1,2,3],[4,5,6],[7,8,9]]]
>>> foo
array[[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]]
>>> random.shuffle[foo]
>>> foo
array[[[1, 2, 3],
[1, 2, 3],
[4, 5, 6]]]
6>>> foo = np.array[[[1,2,3],[4,5,6],[7,8,9]]]
>>> foo
array[[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]]
>>> random.shuffle[foo]
>>> foo
array[[[1, 2, 3],
[1, 2, 3],
[4, 5, 6]]]
7 >>> foo = np.array[[[1,2,3],[4,5,6],[7,8,9]]]
>>> foo
array[[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]]
>>> random.shuffle[foo]
>>> foo
array[[[1, 2, 3],
[1, 2, 3],
[4, 5, 6]]]
8>>> foo = np.array[[[1,2,3],[4,5,6],[7,8,9]]]
>>> foo
array[[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]]
>>> random.shuffle[foo]
>>> foo
array[[[1, 2, 3],
[1, 2, 3],
[4, 5, 6]]]
9>>> foo = np.array[[[1,2,3],[4,5,6],[7,8,9]]]
>>> foo
array[[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]]
>>> np.random.shuffle[foo]
>>> foo
array[[[1, 2, 3],
[7, 8, 9],
[4, 5, 6]]]
0>>> foo = np.array[[[1,2,3],[4,5,6],[7,8,9]]]
>>> foo
array[[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]]
>>> np.random.shuffle[foo]
>>> foo
array[[[1, 2, 3],
[7, 8, 9],
[4, 5, 6]]]
1>>> foo = np.array[[[1,2,3],[4,5,6],[7,8,9]]]
>>> foo
array[[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]]
>>> np.random.shuffle[foo]
>>> foo
array[[[1, 2, 3],
[7, 8, 9],
[4, 5, 6]]]
0>>> foo = np.array[[[1,2,3],[4,5,6],[7,8,9]]]
>>> foo
array[[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]]
>>> np.random.shuffle[foo]
>>> foo
array[[[1, 2, 3],
[7, 8, 9],
[4, 5, 6]]]
3______>>> foo = np.array[[[1,2,3],[4,5,6],[7,8,9]]]
>>> foo
array[[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]]
>>> np.random.shuffle[foo]
>>> foo
array[[[1, 2, 3],
[7, 8, 9],
[4, 5, 6]]]
9 The original list is : [1, 4, 5, 6, 3] The shuffled list is : [4, 3, 1, 5, 6]0
The original list is : [1, 4, 5, 6, 3] The shuffled list is : [4, 3, 1, 5, 6]1
The original list is : [1, 4, 5, 6, 3] The shuffled list is : [4, 3, 1, 5, 6]2
The original list is : [1, 4, 5, 6, 3] The shuffled list is : [4, 3, 1, 5, 6]3
The original list is : [1, 4, 5, 6, 3] The shuffled list is : [4, 3, 1, 5, 6]4
Is
The original list is : [1, 4, 5, 6, 3] The shuffled list is : [5, 3, 6, 1, 4]0
The original list is : [1, 4, 5, 6, 3] The shuffled list is : [5, 3, 6, 1, 4]1
>>> foo = np.array[[[1,2,3],[4,5,6],[7,8,9]]]
>>> foo
array[[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]]
>>> random.shuffle[foo]
>>> foo
array[[[1, 2, 3],
[1, 2, 3],
[4, 5, 6]]]
7 The original list is : [1, 4, 5, 6, 3] The shuffled list is : [5, 3, 6, 1, 4]3
The original list is : [1, 4, 5, 6, 3] The shuffled list is : [5, 6, 4, 3, 1]5__5555532
Output:
The original list is : [1, 4, 5, 6, 3] The shuffled list is : [5, 3, 6, 1, 4]
The original list is : [1, 4, 5, 6, 3]
The shuffled list is : [5, 3, 6, 1, 4]
0Original List: [1, 2, 3, 4, 5, 6]
Shuffled List: [4, 5, 3, 2, 6, 1]
0
7 >>> foo = np.array[[[1,2,3],[4,5,6],[7,8,9]]]
>>> foo
array[[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]]
>>> random.shuffle[foo]
>>> foo
array[[[1, 2, 3],
[1, 2, 3],
[4, 5, 6]]]
Original List: [1, 2, 3, 4, 5, 6]
Shuffled List: [4, 5, 3, 2, 6, 1]
2
>>> foo = np.array[[[1,2,3],[4,5,6],[7,8,9]]]
>>> foo
array[[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]]
>>> np.random.shuffle[foo]
>>> foo
array[[[1, 2, 3],
[7, 8, 9],
[4, 5, 6]]]
9 The original list is : [1, 4, 5, 6, 3] The shuffled list is : [4, 3, 1, 5, 6]0
Original List: [1, 2, 3, 4, 5, 6] Shuffled List: [4, 5, 3, 2, 6, 1]5 ________ 32 & nbsp;
The original list is : [1, 4, 5, 6, 3] The shuffled list is : [4, 3, 1, 5, 6]3
The original list is : [1, 4, 5, 6, 3] The shuffled list is : [4, 3, 1, 5, 6]4
Python3
>>> foo = np.array[[[1,2,3],[4,5,6],[7,8,9]]]
>>> foo
array[[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]]
>>> random.shuffle[foo]
>>> foo
array[[[1, 2, 3],
[1, 2, 3],
[4, 5, 6]]]
4 >>> foo = np.array[[[1,2,3],[4,5,6],[7,8,9]]]
>>> foo
array[[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]]
>>> random.shuffle[foo]
>>> foo
array[[[1, 2, 3],
[1, 2, 3],
[4, 5, 6]]]
5& nbsp; & nbsp; Phương thức #2: sử dụng Random.shuffle [] & nbsp; Đây là phương thức được khuyến nghị nhất để xáo trộn danh sách. Python trong thư viện ngẫu nhiên của nó cung cấp chức năng sẵn có này mà tại chỗ xáo trộn danh sách. Hạn chế của điều này là đặt hàng danh sách bị mất trong quá trình này. Hữu ích cho các nhà phát triển chọn tiết kiệm thời gian và hối hả. & Nbsp;
>>> foo = np.array[[[1,2,3],[4,5,6],[7,8,9]]]
>>> foo
array[[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]]
>>> np.random.shuffle[foo]
>>> foo
array[[[1, 2, 3],
[7, 8, 9],
[4, 5, 6]]]
9The original list is : [1, 4, 5, 6, 3] The shuffled list is : [4, 3, 1, 5, 6]0
>>> foo = np.array[[[1,2,3],[4,5,6],[7,8,9]]]
>>> foo
array[[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]]
>>> random.shuffle[foo]
>>> foo
array[[[1, 2, 3],
[1, 2, 3],
[4, 5, 6]]]
48>>> foo = np.array[[[1,2,3],[4,5,6],[7,8,9]]]
>>> foo
array[[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]]
>>> random.shuffle[foo]
>>> foo
array[[[1, 2, 3],
[1, 2, 3],
[4, 5, 6]]]
49& nbsp; & nbsp; Phương thức #3: Sử dụng Random.sample [] & nbsp; Đây là một hàm khá hữu ích, tốt hơn phương thức xáo trộn được sử dụng ở trên trong khía cạnh mà nó tạo ra một danh sách xáo trộn mới và trả về nó thay vì làm phiền thứ tự của danh sách gốc . Điều này rất hữu ích trong các trường hợp chúng tôi yêu cầu giữ lại danh sách ban đầu. & NBSP;
>>> foo = np.array[[[1,2,3],[4,5,6],[7,8,9]]]
>>> foo
array[[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]]
>>> random.shuffle[foo]
>>> foo
array[[[1, 2, 3],
[1, 2, 3],
[4, 5, 6]]]
18>>> foo = np.array[[[1,2,3],[4,5,6],[7,8,9]]]
>>> foo
array[[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]]
>>> random.shuffle[foo]
>>> foo
array[[[1, 2, 3],
[1, 2, 3],
[4, 5, 6]]]
7 >>> foo = np.array[[[1,2,3],[4,5,6],[7,8,9]]]
>>> foo
array[[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]]
>>> random.shuffle[foo]
>>> foo
array[[[1, 2, 3],
[1, 2, 3],
[4, 5, 6]]]
20The original list is : [1, 4, 5, 6, 3] The shuffled list is : [5, 6, 4, 3, 1]0
The original list is : [1, 4, 5, 6, 3] The shuffled list is : [4, 3, 1, 5, 6]4
>>> foo = np.array[[[1,2,3],[4,5,6],[7,8,9]]]
>>> foo
array[[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]]
>>> np.random.shuffle[foo]
>>> foo
array[[[1, 2, 3],
[7, 8, 9],
[4, 5, 6]]]
9 The original list is : [1, 4, 5, 6, 3] The shuffled list is : [4, 3, 1, 5, 6]0
Original List: [1, 2, 3, 4, 5, 6] Shuffled List: [4, 5, 3, 2, 6, 1]5 ________ 32 & nbsp;
The original list is : [1, 4, 5, 6, 3] The shuffled list is : [4, 3, 1, 5, 6]3
>>> foo = np.array[[[1,2,3],[4,5,6],[7,8,9]]]
>>> foo
array[[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]]
>>> random.shuffle[foo]
>>> foo
array[[[1, 2, 3],
[1, 2, 3],
[4, 5, 6]]]
28The original list is : [1, 4, 5, 6, 3] The shuffled list is : [5, 3, 6, 1, 4]0
>>> foo = np.array[[[1,2,3],[4,5,6],[7,8,9]]]
>>> foo
array[[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]]
>>> random.shuffle[foo]
>>> foo
array[[[1, 2, 3],
[1, 2, 3],
[4, 5, 6]]]
69>>> foo = np.array[[[1,2,3],[4,5,6],[7,8,9]]]
>>> foo
array[[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]]
>>> random.shuffle[foo]
>>> foo
array[[[1, 2, 3],
[1, 2, 3],
[4, 5, 6]]]
7>>> foo = np.array[[[1,2,3],[4,5,6],[7,8,9]]]
>>> foo
array[[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]]
>>> random.shuffle[foo]
>>> foo
array[[[1, 2, 3],
[1, 2, 3],
[4, 5, 6]]]
71The original list is : [1, 4, 5, 6, 3] The shuffled list is : [5, 3, 6, 1, 4]0
>>> foo = np.array[[[1,2,3],[4,5,6],[7,8,9]]]
>>> foo
array[[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]]
>>> random.shuffle[foo]
>>> foo
array[[[1, 2, 3],
[1, 2, 3],
[4, 5, 6]]]
73Phương pháp 4:
Trong phương thức này, chúng tôi chọn một chỉ mục ngẫu nhiên và nối phần tử đó vào chỉ mục đó vào danh sách.
Original List: [1, 2, 3, 4, 5, 6] Shuffled List: [4, 5, 3, 2, 6, 1]