Reputation: 349
My dictionary is like this:
query = {'fowl': [{'cateogry': 'Space'}, {'cateogry': 'Movie'}, {'cateogry': 'six'}], u'Year of the Monkey': {'score': 40, 'match': [{'category': u'Movie'}, {'category': 'heaven'}, {'category': 'released'}]}}
fowl
and Year of the Monkey
are two entities in this. I am trying to extract all category
values for both entities separately with no luck.
None of these work:
query[0] # I was expecting values for fowl
query[0]['category'] # I was expecting all category for fowl but seems wrong
query[0]['category'][0] # category space for fowl
what is the correct approach?
Upvotes: 0
Views: 101
Reputation: 14126
Well, your query
dictionary is rather funky, for one, 'fowl'
and 'Year of the Monkey'
values are not structured the same, so you cannot aply the same data access patterns, or categories being misspelled as 'cateogry'
. If you can, you may be better off fixing that before trying to process it further.
As for extracting the 'fowl'
data:
>>> query = {'fowl': [{'cateogry': 'Space'}, {'cateogry': 'Movie'}, {'cateogry': 'six'}], u'Year of the Monkey': {'score': 40, 'match': [{'category': u'Movie'}, {'category': 'heaven'}, {'category': 'released'}]}}
>>> query['fowl'] # 'fowl'
[{'cateogry': 'Space'}, {'cateogry': 'Movie'}, {'cateogry': 'six'}]
>>> [d['cateogry'] for d in query['fowl']] # 'fowl' categories
['Space', 'Movie', 'six']
>>> [d['cateogry'] for d in query['fowl']][0] # 'fowl' 'Space' category
'Space'
Upvotes: 2
Reputation: 862
query['Year of the Monkey']['match'][0]['category']
you need to iterate
Upvotes: 1