Thursday 15 September 2011

dictionary - Python: Accessing elements from nested dictionaries a la SQL -


A simple question - but I have not found its syntax yet. How do I select the sub-word that has a specific key-value combination? Effectively, how do I select where id = 'ABC' ?

Description:

I am in Python and I have a nested dictionary, I have to get information from the sub-dictionary where the main values ​​of the sub-dictionary One of the blah is.

I have started the dictionary and I can read its elements, but I can not understand how I can call each sub-dictionary with a key value pair. For example, for example, ij category_ data is a Jason object data {i} for category (10): data [i] = {'id': json_data [i] ['id'], 'stationName': json_data [I] ['stationName'], 'numbikes': json_data [i] ['numbikes']}

This has started my nested dictionary, 0] and it will print my sub-dictionary correctly, but since I can add / remove lines, those indices have no meaning, so I him Need access to a significant value.

But when I am equal to the ID "ABC", how do I pull the name of the station and the number of the bike?

Since, as you say, there is no point in the index, why not your id values Use as the key?

  data = {} for category i (10): data [json_data [i] ['id']] = {'stationName': json_data [i] ['stationName'] 'Mailback': json_data [i] ['numbikes']}   

Or, because we are in dragon: id = json_data.items () Data <=> data [id] = {'stationName': value ['StationName'] 'numbikes': value ['numbikes']}

No comments:

Post a Comment