简体   繁体   中英

Python List: Weird Behavior When Disaggregating Dictionary Field

I have data stored in the following format:

name    address    id
person   place     A123
person2  place2    A345
person3  place3    A445;A456
person4  place4    A333

I want to convert it to:

name    address    id
person   place     A123
person2  place2    A345
person3  place3    A445
person3  place3    A456
person4  place4    A333

I'm trying to do it with this:

    combined_file_array = []
    for index, row in enumerate(data):
        if (';' not in row['id']):
            combined_file_array.append(row)
        else:
            ids = row['id'].split(';')
            for id in ids:
                combined_file_array.append(row)
                combined_file_array[-1]['id'] = id.strip()

This code produces the equivalent of:

name    address    id
person   place     A123
person2  place2    A345
person3  place3    A456
person3  place3    A456
person4  place4    A333

Why isn't this working?

You're mutating the same dictionary, so you end up changing the id for both of your rows.

By doing

combined_file_array[-1]['id'] = id.strip()

you're not only changing combined_file_array[-1]['id'] but also combined_file_array[-2]['id'] because they both point towards the same dict.

By appending the same dict row in each iteration over ids and updating the id key to the same dict, you overwrite the value of the id key in the previous iterations.

You should append a new copy of the dict from row instead. Since you are going to update the id key, you can pop it first, and then use generic unpacking to update its value:

for id in row.pop('id').split(';'):
    combined_file_array.append({**row, 'id': id})

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