mardi 18 juillet 2017

Randomly selecting a different pair of items from a list

Code:

x = ['A','B','C','D','E','F',
     'G','H','I','J','K','L',
     'M','N','O','P','Q','R',
     'S','T','U','V','W','X',
     'Y','Z']

y1 = random.sample(x, 2)
y2 = random.sample(x, 2)
y3 = random.sample(x, 2)
y4 = random.sample(x, 2)
y5 = random.sample(x, 2)

Query

As shown above, I'm selecting 5 random sample combinations and declaring them under the variables y'x'.

To improve my code, I would like to do so, but ensure that an item from the list doesn't appear more than once in all variable outputs, in which all combinations are different and non-repetitive. I would preferably like to achieve this without having to remove items from the list as it is reused later in the code.

Expected Output (Example):

>>> y1
['A', 'Q']
>>> y2
['E', 'K']
>>> y3
['C', 'O']
>>> y4
['Z', 'X']
>>> y5
['P', 'L']

Thanks in advance!




Aucun commentaire:

Enregistrer un commentaire