Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Python tell me to 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.

like image 814
no comment Avatar asked Oct 24 '25 04:10

no comment


1 Answers

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.

like image 183
JeffUK Avatar answered Oct 26 '25 18:10

JeffUK