Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why should I sort before taking a random sample?

Python just gave me weird advice:

>>> import random
>>> random.sample({1: 2, 3: 4, 5: 6}, 2)
Traceback (most recent call last):
  File "<pyshell#11>", line 1, in <module>
    random.sample({1: 2, 3: 4, 5: 6}, 2)
  File "C:\Users\*****\AppData\Local\Programs\Python\Python310\lib\random.py", line 466, in sample
    raise TypeError("Population must be a sequence.  For dicts or sets, use sorted(d).")
TypeError: Population must be a sequence.  For dicts or sets, use sorted(d).

Note the second part of the last error line. Why should I sort if I’m going to randomize a moment later anyway? Seems like wasting O(n log n) time.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

From the commit history of cpython – My emphasis: github

In the future, the population must be a sequence. Instances of
:class:set are no longer supported. The set must first be converted
to a :class:list or :class:tuple, preferably in a
deterministic order so that the sample is reproducible.

If you don’t care about reproducibility sorting is not necessary.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading