samedi 20 juin 2020

Python - randomize value for given key and then ranzomize again if there are any repeated keys

I am using the following dict of random values:

dict_positionxy = {'a difficult anglethe left':{'y':random.randrange(43,54), 'x':random.randrange(0,6)},
                   'a difficult anglethe right':{'y':random.randrange(14,25), 'x':random.randrange(0,6)},
                   'long rangeN/A': {'y':random.randrange(0,68), 'x':random.randrange(40,52)},
                   'long rangethe centre':{'y':random.randrange(0,68), 'x':random.randrange(28,40)},
                   'long rangethe left': {'y':random.randrange(54,68), 'x':random.randrange(0,24)},
                   'long rangethe right':{'y':random.randrange(0,14), 'x':random.randrange(0,24)},
                   'outside the boxN/A':{'y':random.randrange(14,54), 'x':random.randrange(16,28)},
                   'penaltyN/A':{'y':random.randrange(36), 'x':random.randrange(8)},
                   'the boxthe centre':{'y':random.randrange(25,45), 'x':random.randrange(0,6)},
                   'the boxthe left':{'y':random.randrange(41,54), 'x':random.randrange(0,16)},
                   'the boxthe right':{'y':random.randrange(14,22), 'x':random.randrange(0,16)},
                   'the six yard boxthe left': {'y':random.randrange(33,43), 'x':random.randrange(4,6)},
                   'the six yard boxthe right':{'y':random.randrange(25,33), 'x':random.randrange(4,6)},
                   'very close rangeN/A':{'y':random.randrange(25,43), 'x':random.randrange(0,4)}}

in order to populate my X/Y grid values, like so:

df_temp_shots['PositionXY'] = df_temp_shots['Position'].map(dict_positionxy)

The problem is that some event keys will be repeated.

For instance if two goals have the same key 'the boxthe left' they will get the same value, which is undesired, because those will be plotted on the same spot.


So how do I randomize any repeated values again, always ending up with unique values within the desired range, for each key?




Aucun commentaire:

Enregistrer un commentaire