lundi 22 février 2021

Replace pairs in the list with other possible pairs randomly

I am working on a problem where I have to calculate how many times a specific element in EB pairs with an element in EA by a fair random choice with a little twist. But before that I used random.sample, to get all the possible pairs from both lists. Then, I multiplied the pairs (repeated) to reach the sample number 100000 and selected 1% of the pairs randomly in the list.

import random
import itertools
EA=["A1","A2","A3","A4"]
EB=["B1","B6","B8","B2","B4","B89","B42","B28","B7","B96","B34","B5","B100","B55","B56","B95","B96","B20"]
EAB=random.sample(set(itertools.product(EA, EB)),72 )
First_Event=EAB*100000
percent_1 = random.sample(First_Event, 1000)
percent_1[1] #this gives for example [('A1', 'B2')] 

Now I want to replace for every pair in the percent_1 list, an element from the EA list eg: A1 in this case by either of its downstream elements randomly ("A2", "A3", "A4"), and element from EB list eg: B2 in this case, can be replaced with ant of the upstream entries ("B1","B6","B8") in EB list. Likewise I want to replace all pairs randomly with their respective EA(Downstream) and EB(Upstream) partners in percent_1 list. If it is (A4,B1) I would retain it as such in the list.




Aucun commentaire:

Enregistrer un commentaire