lundi 11 octobre 2021

Return list of weighted objects with semi-randomized ranking

Let's say I have a list of objects (in Python) that looks something like this (contains an identifier and a ranking/weighting):

objects = [
    ("object_1", 0.50),
    ("object_2", 0.75),
    ("object_3", 0.25),
    ("object_4", 0.01),
    ("object_5", 0.99),
]

I would like to return to the caller this same objects array but in semi-randomized order of their weighting. That is, I don't always want to return:

[
    ("object_5", 0.99),
    ("object_2", 0.75),
    ("object_1", 0.50),
    ("object_3", 0.25),
    ("object_4", 0.01),
]

but would instead rather allow for some non-determinism so that, generally speaking, the returned array looks like the above but could also look like:

[
    ("object_5", 0.99),
    ("object_1", 0.50),
    ("object_2", 0.75),
    ("object_4", 0.01),
    ("object_3", 0.25),
]



Aucun commentaire:

Enregistrer un commentaire