Python: extracting a list from an array of dictionaries with arrays in them
This question is a bit of a convoluted brain-twister, I'm afraid. I'm writing a function test on an api, that when I query it, returns a bunch of json with embedded lists. Here is a significant fragment of what that looks like (with all the data anonymized for this question):
[{u'account': {u'account_name': u'Autotest Account',
u'account_uid': u'000000000'},
u'address': {u'city': u'AutoTest City',
u'country': u'United States',
u'postal_code': u'10019',
u'province': None,
u'state': u'IL',
u'street': [u'12 Auto Road']},
u'children': [{u'institution_name': u'Autotest Bottom Institution 1',
u'institution_type': 1,
u'institution_uid': u'111111111'},
{u'institution_name': u'Autotest Bottom Institution 2',开发者_StackOverflow中文版
u'institution_type': 1,
u'institution_uid': u'222222222'},
{u'institution_name': u'Autotest Bottom Institution 3',
u'institution_type': 1,
u'institution_uid': u'333333333'},
{u'institution_name': u'Autotest Bottom Institution 4',
u'institution_type': 1,
u'institution_uid': u'444444444'},
{u'institution_name': u'Autotest Bottom Institution 5',
u'institution_type': 1,
u'institution_uid': u'555555555'},
{u'institution_name': u'Autotest Bottom Institution 6',
u'institution_type': 1,
u'institution_uid': u'666666666'},
{u'institution_name': u'Autotest Bottom Institution 7',
u'institution_type': 1,
u'institution_uid': u'777777777'},
{u'institution_name': u'Autotest Bottom Institution 8',
u'institution_type': 1,
u'institution_uid': u'888888888'}],
u'institution_name': u'Autotest Middle Institution 1',
u'institution_type': 2,
u'institution_uid': u'000000001',
u'parent': {u'institution_name': u'Autotest Top Institution',
u'institution_type': 3,
u'institution_uid': u'000000099'},
u'school_year': 2011},
{u'account': {u'account_name': u'Autotest Account',
u'account_uid': u'000000000'},
u'address': {u'city': u'Bacon City',
u'country': u'United States',
u'postal_code': u'10018',
u'province': None,
u'state': u'IL',
u'street': [u'15 Wonder Road']},
u'children': [],
u'institution_name': u'Autotest Bottom Institution 1',
u'institution_type': 1,
u'institution_uid': u'111111111',
u'parent': {u'institution_name': u'Autotest Middle Institution 1',
u'institution_type': 2,
u'institution_uid': u'000000001'},
u'school_year': 2011}]
What I'm trying to accomplish, is to extract all of the "Bottom Institution" names from the JSON, and put them into a list that I can then compare to a list that's already in my test fixture data. It should look something like this:
['Autotest Bottom Institution 1','Autotest Bottom Institution 2','Autotest Bottom Institution 3','Autotest Bottom Institution 4','Autotest Bottom Institution 5','Autotest Bottom Institution 6','Autotest Bottom Institution 7','Autotest Bottom Institution 8']
I'm able to extract them one-at-a-time or via iteration, after loading the data into "inst_array", like this:
>>> print inst_array[0]['children'][0]['institution_name']
Autotest Bottom Institution 1
>>> print inst_array[0]['children'][1]['institution_name']
Autotest Bottom Institution 2
>>> print inst_array[0]['children'][2]['institution_name']
Autotest Bottom Institution 3
But here's the kicker: I want to be able to do this without iteration (or, with as little iteration as possible), and because of the umpteen layers of nesting in this, It's got me puzzled.
Any thoughts?
Shouldn't something like this work?
names = [child['institution_name'] for child in inst_array[0]['children']]
I guess a list comprehension is still a form of iteration, but at least concise:
your_list = [elem['institution_name'] for elem in inst_array[0]['children']]
Not sure about what you mean by 'without iteration'. Whatever you do here has to involve iterating through the lists, by definition.
Anyway, here's an attempt:
institutions = set()
for account in data:
for child in account['children']:
institutions.add(child['institution_name'])
I'm not sure what do you mean exactly with "without iteration", but here is something with implied iteration that could work:
[x['institution_name'] for x in inst_array[0]['children'] if x['institution_name']]
Something like:
[ x['institution_name'] for x in inst_array[0]['children'] ]
精彩评论