Frage

I have dictionary like:

Info = {
  "City_Name" : {
    "Population" : None,
    "Population_Density" : None
    }
}

I want to assign values to "Population" and "Population_Density" keys. I actually can do that with the use of the following commands:

Info["City_Name"]["Population"] = 20000 
Info["City_Name"]["Population_Density"] = 200 

But instead, I want to do that with a single command like:

Info["City_Name"]["Population","Population_Density"] = 20000 , 200

But this doesn't work, the command above generates a new key... (In fact, a function returns me those values, and therefore, I need to do that with a single command)

Edit:

I needed to mention; without using:

Info["City_Name"]["Population"],Info["City_Name"]["Population_Density"] = 20000, 200

The key-names of my dictionary are so long that, it is hard to follow; they take a lot of space. I also need to assig three values to three keys. Therefore, I was wondering if there is any way to do that with just a single modification on the part, that is different than each other (eg; "Population" and "Population_Density").

War es hilfreich?

Lösung 2

Try this:

Info["City_Name"].update({"Population": 20000, "Population_Density": 200})

Andere Tipps

Only way to do exactly what you're asking is:

Info["City_Name"]["Population"], Info["City_Name"]["Population_Density"] = 20000, 200

Otherwise it takes Info["City_Name"], and creates a new key ("Population", "Population_Density") (a tuple), and assigns another tuple, (20000, 200) to that

Or you can do it in two lines:

d = Info["City_Name"]
d["Population"], d["Population_Density"] = 20000, 200
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top