jeudi 11 mars 2021

Random pairs without repeats in Python (numpy or itertools)

I have a list of people and want to generate a number of random pairs or triplets so that every person in the list belongs to exactly one pair or triplet (no more, no less).

Example:

>>> people = ["John", "Paul", "George", "Ringo", "David", "Roger", "Richard", "Nick", "Syd"]

>>> generate_random_pairs(people)

[
    ("John", "George"),
    ("Paul", "David"),
    ("Roger", "Nick"),
    ("Ringo", "Richard", "Syd")
]

I've tried different ideas using numpy.random.choice, numpy.random.sample or itertools.permutations but none of them seem to work.




Aucun commentaire:

Enregistrer un commentaire