Reputation: 3927
I am parsing some information from a CSV File and am inputting it into a dict of dicts. The inner dict v
contains the following elements {'140725AD4': <mod.City object at 1x3259C2D1>, '631315AD2': <mod.City object at 0x023A4870>}
. How would I access the object <mod.city object at 0x0138C3B0>
for example?
Thank You.
Upvotes: 0
Views: 63
Reputation: 734
Having a structure like the following:
john = {
"name": "John",
"family": {
"son": "Ret",
"daughter": "Pat"
}
}
You can access the John son's name like this:
john['family']['son']
This will return "Ret"
In your case, if City object is a DICT you can use:
dict['140725AD4']['population']
If your City object is just a Class you can do
dict['140725AD4'].getPopulation()
or
dict['140725AD4'].population
How it works? A dictionary is a hashmap of pairs (name, value). Whenever you call a name the value is given. In Python, the value can be anything, from int to a Class.
So when in a dict you ask for a name inside a dict like dict['name'] you get back the value associated with it. In this case its your City object. Then you can call anything related to the value: functions, variables...
Upvotes: 3
Reputation: 12092
Assuming this question is the follow up for this one - Updating Dictionary of Dictionaries in Python you would have to do this:
inner_dict = places["City"]
for _, city_obj in inner_dict.items():
print city_obj.population # Assuming population is one of the member variables of the class mod.city
Upvotes: 0