Mẫu [] là một hàm sẵn có của mô -đun ngẫu nhiên trong Python trả về một danh sách độ dài cụ thể của các mục được chọn từ chuỗi, tức là danh sách, tuple, chuỗi hoặc bộ. Được sử dụng để lấy mẫu ngẫu nhiên mà không cần thay thế. is an inbuilt function of random module in Python that returns a particular length list of items chosen from the sequence i.e. list, tuple, string or set. Used for random sampling without replacement.
Cú pháp: ngẫu nhiên.sample [trình tự, k] random.sample[sequence, k]
Tham số: Trình tự: Có thể là một danh sách, tuple, chuỗi hoặc set.k: Giá trị số nguyên, nó chỉ định độ dài của mẫu.
sequence: Can be a list, tuple, string, or set.
k: An Integer value, it specify the length of a sample.Trả về: K LEGN DANH SÁCH MỚI Các yếu tố được chọn từ chuỗi. k length new list of elements chosen from the sequence.
Mã số 1: Thực hiện đơn giản hàm sample []. Simple implementation of sample[] function.
from
random
import
With list: [3, 1, 2] With string: ['e', 'f', 'G', 'G'] With tuple: ['ankit', 'portal', 'geeks', 'computer'] With set: ['b', 'd', 'c']0
With list: [3, 1, 2] With string: ['e', 'f', 'G', 'G'] With tuple: ['ankit', 'portal', 'geeks', 'computer'] With set: ['b', 'd', 'c']1
With list: [3, 1, 2] With string: ['e', 'f', 'G', 'G'] With tuple: ['ankit', 'portal', 'geeks', 'computer'] With set: ['b', 'd', 'c']2
With list: [3, 1, 2] With string: ['e', 'f', 'G', 'G'] With tuple: ['ankit', 'portal', 'geeks', 'computer'] With set: ['b', 'd', 'c']3
With list: [3, 1, 2] With string: ['e', 'f', 'G', 'G'] With tuple: ['ankit', 'portal', 'geeks', 'computer'] With set: ['b', 'd', 'c']4
With list: [3, 1, 2] With string: ['e', 'f', 'G', 'G'] With tuple: ['ankit', 'portal', 'geeks', 'computer'] With set: ['b', 'd', 'c']5
With list: [3, 1, 2] With string: ['e', 'f', 'G', 'G'] With tuple: ['ankit', 'portal', 'geeks', 'computer'] With set: ['b', 'd', 'c']6
With list: [3, 1, 2] With string: ['e', 'f', 'G', 'G'] With tuple: ['ankit', 'portal', 'geeks', 'computer'] With set: ['b', 'd', 'c']5
With list: [3, 1, 2] With string: ['e', 'f', 'G', 'G'] With tuple: ['ankit', 'portal', 'geeks', 'computer'] With set: ['b', 'd', 'c']8
With list: [3, 1, 2] With string: ['e', 'f', 'G', 'G'] With tuple: ['ankit', 'portal', 'geeks', 'computer'] With set: ['b', 'd', 'c']5
Traceback [most recent call last]: File "C:/Users/user/AppData/Local/Programs/Python/Python36/all_prgm/geeks_article/sample_method_article.py", line 8, in print[random.sample[list1, 5]] File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\random.py", line 317, in sample raise ValueError["Sample larger than population or is negative"] ValueError: Sample larger than population or is negative0
With list: [3, 1, 2] With string: ['e', 'f', 'G', 'G'] With tuple: ['ankit', 'portal', 'geeks', 'computer'] With set: ['b', 'd', 'c']5
Traceback [most recent call last]: File "C:/Users/user/AppData/Local/Programs/Python/Python36/all_prgm/geeks_article/sample_method_article.py", line 8, in print[random.sample[list1, 5]] File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\random.py", line 317, in sample raise ValueError["Sample larger than population or is negative"] ValueError: Sample larger than population or is negative223
Traceback [most recent call last]: File "C:/Users/user/AppData/Local/Programs/Python/Python36/all_prgm/geeks_article/sample_method_article.py", line 8, in print[random.sample[list1, 5]] File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\random.py", line 317, in sample raise ValueError["Sample larger than population or is negative"] ValueError: Sample larger than population or is negative4
Traceback [most recent call last]: File "C:/Users/user/AppData/Local/Programs/Python/Python36/all_prgm/geeks_article/sample_method_article.py", line 8, in print[random.sample[list1, 5]] File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\random.py", line 317, in sample raise ValueError["Sample larger than population or is negative"] ValueError: Sample larger than population or is negative5
With list: [3, 1, 2] With string: ['e', 'f', 'G', 'G'] With tuple: ['ankit', 'portal', 'geeks', 'computer'] With set: ['b', 'd', 'c']8
Traceback [most recent call last]: File "C:/Users/user/AppData/Local/Programs/Python/Python36/all_prgm/geeks_article/sample_method_article.py", line 8, in print[random.sample[list1, 5]] File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\random.py", line 317, in sample raise ValueError["Sample larger than population or is negative"] ValueError: Sample larger than population or is negative7
Output:
[2, 3, 5]
& nbsp; Mã số 2: Hàm sử dụng cơ bản của mẫu [].
Code
#2: Basic use of sample[] function.
import
Traceback [most recent call last]: File "C:/Users/user/AppData/Local/Programs/Python/Python36/all_prgm/geeks_article/sample_method_article.py", line 8, in print[random.sample[list1, 5]] File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\random.py", line 317, in sample raise ValueError["Sample larger than population or is negative"] ValueError: Sample larger than population or is negative9
With list: [3, 1, 2] With string: ['e', 'f', 'G', 'G'] With tuple: ['ankit', 'portal', 'geeks', 'computer'] With set: ['b', 'd', 'c']1
With list: [3, 1, 2] With string: ['e', 'f', 'G', 'G'] With tuple: ['ankit', 'portal', 'geeks', 'computer'] With set: ['b', 'd', 'c']2
With list: [3, 1, 2] With string: ['e', 'f', 'G', 'G'] With tuple: ['ankit', 'portal', 'geeks', 'computer'] With set: ['b', 'd', 'c']3
With list: [3, 1, 2] With string: ['e', 'f', 'G', 'G'] With tuple: ['ankit', 'portal', 'geeks', 'computer'] With set: ['b', 'd', 'c']4
With list: [3, 1, 2] With string: ['e', 'f', 'G', 'G'] With tuple: ['ankit', 'portal', 'geeks', 'computer'] With set: ['b', 'd', 'c']5
With list: [3, 1, 2] With string: ['e', 'f', 'G', 'G'] With tuple: ['ankit', 'portal', 'geeks', 'computer'] With set: ['b', 'd', 'c']6
With list: [3, 1, 2] With string: ['e', 'f', 'G', 'G'] With tuple: ['ankit', 'portal', 'geeks', 'computer'] With set: ['b', 'd', 'c']5
With list: [3, 1, 2] With string: ['e', 'f', 'G', 'G'] With tuple: ['ankit', 'portal', 'geeks', 'computer'] With set: ['b', 'd', 'c']8
With list: [3, 1, 2] With string: ['e', 'f', 'G', 'G'] With tuple: ['ankit', 'portal', 'geeks', 'computer'] With set: ['b', 'd', 'c']5
Traceback [most recent call last]: File "C:/Users/user/AppData/Local/Programs/Python/Python36/all_prgm/geeks_article/sample_method_article.py", line 8, in print[random.sample[list1, 5]] File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\random.py", line 317, in sample raise ValueError["Sample larger than population or is negative"] ValueError: Sample larger than population or is negative0
With list: [3, 1, 2] With string: ['e', 'f', 'G', 'G'] With tuple: ['ankit', 'portal', 'geeks', 'computer'] With set: ['b', 'd', 'c']5
Traceback [most recent call last]: File "C:/Users/user/AppData/Local/Programs/Python/Python36/all_prgm/geeks_article/sample_method_article.py", line 8, in print[random.sample[list1, 5]] File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\random.py", line 317, in sample raise ValueError["Sample larger than population or is negative"] ValueError: Sample larger than population or is negative22215
>>> c = range[0, 15]
>>> c
range[0, 15]
>>> random.sample[c, 5]
[12, 3, 6, 14, 10]
3Traceback [most recent call last]: File "C:/Users/user/AppData/Local/Programs/Python/Python36/all_prgm/geeks_article/sample_method_article.py", line 8, in print[random.sample[list1, 5]] File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\random.py", line 317, in sample raise ValueError["Sample larger than population or is negative"] ValueError: Sample larger than population or is negative3
Traceback [most recent call last]: File "C:/Users/user/AppData/Local/Programs/Python/Python36/all_prgm/geeks_article/sample_method_article.py", line 8, in print[random.sample[list1, 5]] File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\random.py", line 317, in sample raise ValueError["Sample larger than population or is negative"] ValueError: Sample larger than population or is negative4
>>> c = range[0, 15]
>>> c
range[0, 15]
>>> random.sample[c, 5]
[12, 3, 6, 14, 10]
6>>> c = range[0, 15]
>>> c
range[0, 15]
>>> random.sample[c, 5]
[12, 3, 6, 14, 10]
7>>> c = range[0, 15]
>>> c
range[0, 15]
>>> random.sample[c, 5]
[12, 3, 6, 14, 10]
8With list: [3, 1, 2] With string: ['e', 'f', 'G', 'G'] With tuple: ['ankit', 'portal', 'geeks', 'computer'] With set: ['b', 'd', 'c']8
Traceback [most recent call last]: File "C:/Users/user/AppData/Local/Programs/Python/Python36/all_prgm/geeks_article/sample_method_article.py", line 8, in print[random.sample[list1, 5]] File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\random.py", line 317, in sample raise ValueError["Sample larger than population or is negative"] ValueError: Sample larger than population or is negative7
>>> c = {1, 2, 4}
>>> random.sample[c, 2]
[4, 1]
1With list: [3, 1, 2] With string: ['e', 'f', 'G', 'G'] With tuple: ['ankit', 'portal', 'geeks', 'computer'] With set: ['b', 'd', 'c']2
>>> c = {1, 2, 4}
>>> random.sample[c, 2]
[4, 1]
3Traceback [most recent call last]: File "C:/Users/user/AppData/Local/Programs/Python/Python36/all_prgm/geeks_article/sample_method_article.py", line 8, in print[random.sample[list1, 5]] File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\random.py", line 317, in sample raise ValueError["Sample larger than population or is negative"] ValueError: Sample larger than population or is negative4
>>> c = range[0, 15]
>>> c
range[0, 15]
>>> random.sample[c, 5]
[12, 3, 6, 14, 10]
6>>> c = {1, 2, 4}
>>> random.sample[c, 2]
[4, 1]
6>>> c = {1, 2, 4}
>>> random.sample[c, 2]
[4, 1]
7Traceback [most recent call last]: File "C:/Users/user/AppData/Local/Programs/Python/Python36/all_prgm/geeks_article/sample_method_article.py", line 8, in print[random.sample[list1, 5]] File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\random.py", line 317, in sample raise ValueError["Sample larger than population or is negative"] ValueError: Sample larger than population or is negative0
Traceback [most recent call last]: File "C:/Users/user/AppData/Local/Programs/Python/Python36/all_prgm/geeks_article/sample_method_article.py", line 8, in print[random.sample[list1, 5]] File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\random.py", line 317, in sample raise ValueError["Sample larger than population or is negative"] ValueError: Sample larger than population or is negative7
>>> c = [1, 3]
>>> random.sample[iter[c], 5]
TypeError: Population must be a sequence or set. For dicts, use list[d].
0With list: [3, 1, 2] With string: ['e', 'f', 'G', 'G'] With tuple: ['ankit', 'portal', 'geeks', 'computer'] With set: ['b', 'd', 'c']2
>>> c = range[0, 15]
>>> c
range[0, 15]
>>> random.sample[c, 5]
[12, 3, 6, 14, 10]
6>>> c = [1, 3]
>>> random.sample[iter[c], 5]
TypeError: Population must be a sequence or set. For dicts, use list[d].
3With list: [3, 1, 2] With string: ['e', 'f', 'G', 'G'] With tuple: ['ankit', 'portal', 'geeks', 'computer'] With set: ['b', 'd', 'c']5
>>> c = [1, 3]
>>> random.sample[iter[c], 5]
TypeError: Population must be a sequence or set. For dicts, use list[d].
5With list: [3, 1, 2] With string: ['e', 'f', 'G', 'G'] With tuple: ['ankit', 'portal', 'geeks', 'computer'] With set: ['b', 'd', 'c']5
>>> c = [1, 3]
>>> random.sample[iter[c], 5]
TypeError: Population must be a sequence or set. For dicts, use list[d].
7With list: [3, 1, 2] With string: ['e', 'f', 'G', 'G'] With tuple: ['ankit', 'portal', 'geeks', 'computer'] With set: ['b', 'd', 'c']5
>>> c = [1, 3]
>>> random.sample[iter[c], 5]
TypeError: Population must be a sequence or set. For dicts, use list[d].
9from
0‘
Traceback [most recent call last]: File "C:/Users/user/AppData/Local/Programs/Python/Python36/all_prgm/geeks_article/sample_method_article.py", line 8, in print[random.sample[list1, 5]] File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\random.py", line 317, in sample raise ValueError["Sample larger than population or is negative"] ValueError: Sample larger than population or is negative4
>>> c = range[0, 15]
>>> c
range[0, 15]
>>> random.sample[c, 5]
[12, 3, 6, 14, 10]
6random
0random
1Traceback [most recent call last]: File "C:/Users/user/AppData/Local/Programs/Python/Python36/all_prgm/geeks_article/sample_method_article.py", line 8, in print[random.sample[list1, 5]] File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\random.py", line 317, in sample raise ValueError["Sample larger than population or is negative"] ValueError: Sample larger than population or is negative0
Traceback [most recent call last]: File "C:/Users/user/AppData/Local/Programs/Python/Python36/all_prgm/geeks_article/sample_method_article.py", line 8, in print[random.sample[list1, 5]] File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\random.py", line 317, in sample raise ValueError["Sample larger than population or is negative"] ValueError: Sample larger than population or is negative7
random
4
With list: [3, 1, 2] With string: ['e', 'f', 'G', 'G'] With tuple: ['ankit', 'portal', 'geeks', 'computer'] With set: ['b', 'd', 'c']2
random
6random
7With list: [3, 1, 2] With string: ['e', 'f', 'G', 'G'] With tuple: ['ankit', 'portal', 'geeks', 'computer'] With set: ['b', 'd', 'c']5
random
9With list: [3, 1, 2] With string: ['e', 'f', 'G', 'G'] With tuple: ['ankit', 'portal', 'geeks', 'computer'] With set: ['b', 'd', 'c']5
With list: [3, 1, 2] With string: ['e', 'f', 'G', 'G'] With tuple: ['ankit', 'portal', 'geeks', 'computer'] With set: ['b', 'd', 'c']
Traceback [most recent call last]: File "C:/Users/user/AppData/Local/Programs/Python/Python36/all_prgm/geeks_article/sample_method_article.py", line 8, in print[random.sample[list1, 5]] File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\random.py", line 317, in sample raise ValueError["Sample larger than population or is negative"] ValueError: Sample larger than population or is negative4
>>> c = range[0, 15]
>>> c
range[0, 15]
>>> random.sample[c, 5]
[12, 3, 6, 14, 10]
6import
9With list: [3, 1, 2] With string: ['e', 'f', 'G', 'G'] With tuple: ['ankit', 'portal', 'geeks', 'computer'] With set: ['b', 'd', 'c']00
With list: [3, 1, 2] With string: ['e', 'f', 'G', 'G'] With tuple: ['ankit', 'portal', 'geeks', 'computer'] With set: ['b', 'd', 'c']8
Traceback [most recent call last]: File "C:/Users/user/AppData/Local/Programs/Python/Python36/all_prgm/geeks_article/sample_method_article.py", line 8, in print[random.sample[list1, 5]] File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\random.py", line 317, in sample raise ValueError["Sample larger than population or is negative"] ValueError: Sample larger than population or is negative7
Output:
With list: [3, 1, 2] With string: ['e', 'f', 'G', 'G'] With tuple: ['ankit', 'portal', 'geeks', 'computer'] With set: ['b', 'd', 'c']
Lưu ý: Đầu ra sẽ khác nhau mọi lúc vì nó trả về một mục ngẫu nhiên. & NBSP; Mã số 3: Tăng ngoại lệOutput will be different everytime as it returns a random item.
Code #3: Raise Exception
Nếu cỡ mẫu, tức là K lớn hơn kích thước trình tự, giá trịerror sẽ được nâng lên.
import
Traceback [most recent call last]: File "C:/Users/user/AppData/Local/Programs/Python/Python36/all_prgm/geeks_article/sample_method_article.py", line 8, in print[random.sample[list1, 5]] File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\random.py", line 317, in sample raise ValueError["Sample larger than population or is negative"] ValueError: Sample larger than population or is negative9
With list: [3, 1, 2] With string: ['e', 'f', 'G', 'G'] With tuple: ['ankit', 'portal', 'geeks', 'computer'] With set: ['b', 'd', 'c']1
With list: [3, 1, 2] With string: ['e', 'f', 'G', 'G'] With tuple: ['ankit', 'portal', 'geeks', 'computer'] With set: ['b', 'd', 'c']2
With list: [3, 1, 2] With string: ['e', 'f', 'G', 'G'] With tuple: ['ankit', 'portal', 'geeks', 'computer'] With set: ['b', 'd', 'c']3
With list: [3, 1, 2] With string: ['e', 'f', 'G', 'G'] With tuple: ['ankit', 'portal', 'geeks', 'computer'] With set: ['b', 'd', 'c']4
With list: [3, 1, 2] With string: ['e', 'f', 'G', 'G'] With tuple: ['ankit', 'portal', 'geeks', 'computer'] With set: ['b', 'd', 'c']5
With list: [3, 1, 2] With string: ['e', 'f', 'G', 'G'] With tuple: ['ankit', 'portal', 'geeks', 'computer'] With set: ['b', 'd', 'c']6
With list: [3, 1, 2] With string: ['e', 'f', 'G', 'G'] With tuple: ['ankit', 'portal', 'geeks', 'computer'] With set: ['b', 'd', 'c']5
With list: [3, 1, 2] With string: ['e', 'f', 'G', 'G'] With tuple: ['ankit', 'portal', 'geeks', 'computer'] With set: ['b', 'd', 'c']8
With list: [3, 1, 2] With string: ['e', 'f', 'G', 'G'] With tuple: ['ankit', 'portal', 'geeks', 'computer'] With set: ['b', 'd', 'c']5
Traceback [most recent call last]: File "C:/Users/user/AppData/Local/Programs/Python/Python36/all_prgm/geeks_article/sample_method_article.py", line 8, in print[random.sample[list1, 5]] File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\random.py", line 317, in sample raise ValueError["Sample larger than population or is negative"] ValueError: Sample larger than population or is negative0
With list: [3, 1, 2] With string: ['e', 'f', 'G', 'G'] With tuple: ['ankit', 'portal', 'geeks', 'computer'] With set: ['b', 'd', 'c']5
Traceback [most recent call last]: File "C:/Users/user/AppData/Local/Programs/Python/Python36/all_prgm/geeks_article/sample_method_article.py", line 8, in print[random.sample[list1, 5]] File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\random.py", line 317, in sample raise ValueError["Sample larger than population or is negative"] ValueError: Sample larger than population or is negative22215
>>> c = range[0, 15]
>>> c
range[0, 15]
>>> random.sample[c, 5]
[12, 3, 6, 14, 10]
3Traceback [most recent call last]: File "C:/Users/user/AppData/Local/Programs/Python/Python36/all_prgm/geeks_article/sample_method_article.py", line 8, in print[random.sample[list1, 5]] File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\random.py", line 317, in sample raise ValueError["Sample larger than population or is negative"] ValueError: Sample larger than population or is negative3
Traceback [most recent call last]: File "C:/Users/user/AppData/Local/Programs/Python/Python36/all_prgm/geeks_article/sample_method_article.py", line 8, in print[random.sample[list1, 5]] File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\random.py", line 317, in sample raise ValueError["Sample larger than population or is negative"] ValueError: Sample larger than population or is negative4
With list: [3, 1, 2] With string: ['e', 'f', 'G', 'G'] With tuple: ['ankit', 'portal', 'geeks', 'computer'] With set: ['b', 'd', 'c']17
Traceback [most recent call last]: File "C:/Users/user/AppData/Local/Programs/Python/Python36/all_prgm/geeks_article/sample_method_article.py", line 8, in print[random.sample[list1, 5]] File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\random.py", line 317, in sample raise ValueError["Sample larger than population or is negative"] ValueError: Sample larger than population or is negative2
With list: [3, 1, 2] With string: ['e', 'f', 'G', 'G'] With tuple: ['ankit', 'portal', 'geeks', 'computer'] With set: ['b', 'd', 'c']19
Output:
Traceback [most recent call last]: File "C:/Users/user/AppData/Local/Programs/Python/Python36/all_prgm/geeks_article/sample_method_article.py", line 8, in print[random.sample[list1, 5]] File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\random.py", line 317, in sample raise ValueError["Sample larger than population or is negative"] ValueError: Sample larger than population or is negative
Theo tài liệu:
ngẫu nhiên.sample [dân số, k]
Trả về một danh sách chiều dài k của các yếu tố duy nhất được chọn từ chuỗi dân số. Được sử dụng để lấy mẫu ngẫu nhiên mà không cần thay thế.
Về cơ bản, nó chọn k các yếu tố ngẫu nhiên duy nhất, một mẫu, từ một chuỗi:
>>> import random
>>> c = list[range[0, 15]]
>>> c
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
>>> random.sample[c, 5]
[9, 2, 3, 14, 11]
With list: [3, 1, 2] With string: ['e', 'f', 'G', 'G'] With tuple: ['ankit', 'portal', 'geeks', 'computer'] With set: ['b', 'd', 'c']20 cũng hoạt động trực tiếp từ một phạm vi:
>>> c = range[0, 15]
>>> c
range[0, 15]
>>> random.sample[c, 5]
[12, 3, 6, 14, 10]
Ngoài các chuỗi,
With list: [3, 1, 2] With string: ['e', 'f', 'G', 'G'] With tuple: ['ankit', 'portal', 'geeks', 'computer'] With set: ['b', 'd', 'c']20 cũng hoạt động với các bộ:
>>> c = {1, 2, 4}
>>> random.sample[c, 2]
[4, 1]
Tuy nhiên,
With list: [3, 1, 2] With string: ['e', 'f', 'G', 'G'] With tuple: ['ankit', 'portal', 'geeks', 'computer'] With set: ['b', 'd', 'c']20 không hoạt động với các trình lặp tùy ý:
>>> c = [1, 3]
>>> random.sample[iter[c], 5]
TypeError: Population must be a sequence or set. For dicts, use list[d].