dimanche 8 décembre 2019

Build unique & random subset from the list

I want create a list of subset which should not allow to duplicated item, here my sample code

import random
idx = (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,21,22,23,24,25,26,27,28,29,30)
n = 3
selected_unique_subset = []
for i in range(n):
    selected_unique_subset.append(set(random.choices(idx, k=int(len(idx) / n))))
print(selected_unique_subset)

OUTPUT: From below list, the subset has duplicated element 2 in the first subset {2, 5, 7, 12, 19, 25, 28, 29} and 2 in last subset{2, 7, 15, 17, 21, 24, 26, 27, 30} , for example

[
{2, 5, 7, 12, 19, 25, 28, 29}, 
{8, 12, 14, 15, 16, 18, 19, 21, 24, 28}, 
{2, 7, 15, 17, 21, 24, 26, 27, 30}
]

EXPECTING:

I want to each subset should not contains duplicated item, it should randomly and uniquely .

Could any give me some advice to solve this challenge?




Aucun commentaire:

Enregistrer un commentaire