Feb-22-2021, 05:33 PM
(Feb-22-2021, 04:34 PM)snippsat Wrote:(Feb-22-2021, 01:03 PM)Gilush Wrote: It's my first time reading about the random.sample option. what is the "k" stands for?random.sample
Basically it picksk
unique random elements(sample) from a sequence.
>>> import random >>> >>> random.sample(range(1, 10), 5) [5, 3, 1, 7, 9] >>> random.sample(range(1, 10), 5) [8, 9, 6, 1, 4] >>> random.sample(range(1, 10), 11) Traceback (most recent call last): File "<interactive input>", line 1, in <module> File "C:\Python39\lib\random.py", line 363, in sample raise ValueError("Sample larger than population or is negative") ValueError: Sample larger than population or is negativeIf doing it like this there will not be unique elements.
>>> [random.randint(1, 10) for i in range(5)] [5, 8, 8, 9, 5] >>> [random.randint(1, 10) for i in range(5)] [9, 1, 3, 5, 5]
Thank you.
That answer helps me understand generators better.