jeudi 24 juin 2021

Setting random values to half of the dictionary and copying the rest

I have a python dictionary created by an other list via:

a = dict.fromkeys(some_list)

Because I created this dictionary via the above command all my values in the dictionary are None as expected. And it looks like this:

a = {('A', 'B'): None,
     ('A', 'C'): None,
     ('B', 'A'): None,
     ('C', 'A'): None}

What I want is: to be able to change the None values to random numbers via random.uniform(0,1).

However I don't wanna do this to whole of the dictionary instead only the first 2 keys (('A', 'B'), ('A', 'C')), and the rest (('B', 'A'), ('C', 'A')) should be copied from the first 2 keys.

Following sets all the values to random numbers. However I could not find a way to set the first two as random and the rest copied without hardcoding the keys.

for key in a:
    a[key] = random.uniform(0, 1)

Any Suggestions?




Aucun commentaire:

Enregistrer un commentaire