简体   繁体   中英

Having trouble understanding List Comprehension in Python

I am trying to wrap my mind around list comprehension based on another question I saw here today. I have customized and simplified the code from the previous question so it isn't blatantly the same. I am trying to step through converting this code to a traditional set of nested for loops to help me understand it:

employee_records = [('group1', {'values': []}),
                    ('group2',
                        {'values': [
                            {'employee': {'name': 'Joseph', 'id': 103, 'displayName': 'Joe'}, 'category': 'Title1'},
                            {'employee': {'name': 'Robert', 'id': 114, 'displayName': 'Bob'}, 'category': 'Title2'}
                        ]}
                    ),
                    ('group3',
                        {'values': [
                            {'employee': {'name': 'Rachel', 'id': 1563, 'displayName': 'Rach'}, 'category': 'title3'}
                        ]}
                    )]

employee_details = [[nested_list[0], x['employee']['name'], x['category']]
                    for nested_list in employee_records
                    for x in nested_list[1]["values"]]
                   
print(employee_details)

The output is as follows:

[['group2', 'Joseph', 'Title1'], ['group2', 'Robert', 'Title2'], ['group3', 'Rachel', 'title3']]

When I translate this (based on my understanding) into a set of nested for loops, I get a different result:

employee_records = [('group1', {'values': []}), ('group2', {'values': [\
               {'employee': {'name': 'Joseph', 'id': 103, 'displayName': 'Joe'}, 'category': 'Title1'}, \
               {'employee': {'name': 'Robert', 'id': 114, 'displayName': 'Bob'}, 'category': 'Title2'}]}), \
               ('group3', {'values': [{'employee': {'name': 'Rachel', 'id': 1563, 'displayName': 'Rach'}, 'category': 'title3'}]})]

department = []

for nested_list in employee_records:
    for x in nested_list[1]['values']:
        department.append([[nested_list][0], x['employee']['name'], x['category']])

print(department)

Output:

[[('group2', {'values': [{'employee': {'name': 'Joseph', 'id': 103, 'displayName': 'Joe'}, 'category': 'Title1'}, {'employee': {'name': 'Robert', 'id': 114, 'displayName': 'Bob'}, 'category': 'Title2'}]}), 'Joseph', 'Title1'], [('group2', {'values': [{'employee': {'name': 'Joseph', 'id': 103, 'displayName': 'Joe'}, 'category': 'Title1'}, {'employee': {'name': 'Robert', 'id': 114, 'displayName': 'Bob'}, 'category': 'Title2'}]}), 'Robert', 'Title2'], [('group3', {'values': [{'employee': {'name': 'Rachel', 'id': 1563, 'displayName': 'Rach'}, 'category': 'title3'}]}), 'Rachel', 'title3']]

While I was troubleshooting this, I was able to get the expected result by changing the append statement to be more specific for the first element:

employee_records = [('group1', {'values': []}), ('group2', {'values': [\
               {'employee': {'name': 'Joseph', 'id': 103, 'displayName': 'Joe'}, 'category': 'Title1'}, \
               {'employee': {'name': 'Robert', 'id': 114, 'displayName': 'Bob'}, 'category': 'Title2'}]}), \
               ('group3', {'values': [{'employee': {'name': 'Rachel', 'id': 1563, 'displayName': 'Rach'}, 'category': 'title3'}]})]

department = []

for nested_list in employee_records:
    for x in nested_list[1]['values']:
        department.append([[nested_list][0][0], x['employee']['name'], x['category']])

print(department)

Output:

[['group2', 'Joseph', 'Title1'], ['group2', 'Robert', 'Title2'], ['group3', 'Rachel', 'title3']]

Why do I need to change nested_list[0] to nested_list[0][0] when refactoring this into nested for loops?

You ask

Why do I need to change nested_list[0] to nested_list[0][0]

but your program does not do nested_list[0] , it does [nested_list][0] , which is just a complicated way of saying nested_list .

More specifically,

Your comprehension is doing:

employee_details = [
    [
        nested_list[0],
        x['employee']['name'],
        x['category']
    ]
    for nested_list in employee_records
    for x in nested_list[1]["values"]
]

While you for loops are doing:

employee_details = []
for nested_list in employee_records:
    for x in nested_list[1]['values']:
        employee_details.append([
            [nested_list][0],    ## <--- extra []
            x['employee']['name'],
            x['category']
        ])

I think I found your minor problem.

For your first trial:

for nested_list in employee_records:
    for x in nested_list[1]['values']:
        department.append([[nested_list][0], x['employee']['name'], x['category']])

print(department)

You are appending a list of a list of the nested_list variable, which is actually causing the different output. Just remove the list from the nested_list and you will get the same result as the list comprehension.

for nested_list in employee_records:
    for x in nested_list[1]['values']:
        department.append([nested_list[0], x['employee']['name'], x['category']])

print(department)

The outer square of the list comprehension is working the same as append([])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM