mercredi 11 décembre 2019

Select a random entry from a dictionary

I have the following python dictionary:

    truthDict = {
     1: False,
    -1: True,
     3: True,
    -3: False,
    -4: False,
     4: True,
     2: True,
    -2: True,
     5: False,
    -5: True
    }

I would like to flip one of the key to the inverse:

I tried this:

    pick = random.choice(truthDict.keys())  
    truthDict[pick] = not(truthDict[pick])

However it does not work, I would really appreciate any help.




Aucun commentaire:

Enregistrer un commentaire