samedi 13 novembre 2021

Change multiple values in a dictionary based on current values

I have a dictionary which I have created from a text file (this works), however I need (key = word, value = category for the word). I have searched for Google and my "Python for Dummies" book for the last 40 minutes and am finding nothing about how to choose one of the categories which are in the values of my dictionary and change all of them to a new value (same dictionary, not create a new one).

Example of data:

Blue colour
Tomato food
Television object

(with blue, tomato and television being the keys, and colour food and object being the values)

Let's say I want to change colour to 1, food to 2 and object to 3 in my dictionary, i.e.:

Blue 1
Tomato 2
Television 3

These aren't the only entries in my dictionary, and so it needs to be something that changes all values of a certain type in one hit.

Is there a way to use a for loop with an if elif loop nested inside to achieve this?

for value in objects.values():
    if value = "colour"
    .............

Or am I totally losing the idea of what I need to do?

Aucun commentaire:

Enregistrer un commentaire