dimanche 17 juillet 2022

Random sampling from large dict in Python [duplicate]

I have a (potentially) huge dict in Python and want to randomly sample a few values. Alas, random.sample(my_dict, k) says:

TypeError: Population must be a sequence.  For dicts or sets, use sorted(d).

and random.sample(my_dict.keys(), k) gives

DeprecationWarning: Sampling from a set deprecated
since Python 3.9 and will be removed in a subsequent version.

I don't want to pay the cost of sorting my dictionary keys, and I don't need them sorted.




Aucun commentaire:

Enregistrer un commentaire