字典拆分/理解

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了字典拆分/理解相关的知识,希望对你有一定的参考价值。

我有一个字典,希望将其拆分成字典列表,以便将提交内容添加到数据库中。

这里是字典,这不是静态字典,它将动态生成,因此数字可以增加

# notice the keys are all grouped by numbers, 

 data = {'resident_payer,1': 'William Brown',
        'Term Fee,amount_paid,1': '1',
        'method,1': 'credit',
        'document_id,1': '1',

        'resident_payer,2': None,
        'Term Fee,amount_paid,2': '0',
        'method,2': 'other',
        'document_id,2': '0'}

我需要一个看起来像这样的字典列表:

[
{'resident_payer': 'William Brown', 'Term Fee,amount_paid': '1', 'method': 'credit', 'document_id': '1'},
{'resident_payer': None, 'Term Fee_amount_paid': '0', 'method': 'other', 'document_id': '0'}
]

如何通过简单的字典理解来做到这一点?

这里是有效的代码,但是我在使用和清除在循环之外声明的变量时似乎没有奇怪的复杂性,所以我想不出解决方案,所以我想用一种更清晰,pythonic的方式编写此代码。

data = {'resident_payer,1': 'William Brown',
        'Term Fee,amount_paid,1': '1',
        'method,1': 'credit',
        'document_id,1': '1',

        'resident_payer,2': None,
        'Term Fee,amount_paid,2': '0',
        'method,2': 'other',
        'document_id,2': '0'}

# will hold broken down lists
list_of_submissions = list()
# used to parse data into separated list of dictionaries.
# The key is split into numbers for grouping
current_loop = 1
active_dict_to_add_to_list = dict()
for key, value in data.items():
    if f'{current_loop}' in key:
        # we are in the current iteration
        # add the item to the active dict, the key is split by the ',' and [1] is the number so [0] needs to be selected
        split = key.split(',')
        if len(split) > 2:
            # we have the dynamic field
            key_to_use = ",".join([split[0], split[1]])
        else:
            key_to_use = split[0]

        active_dict_to_add_to_list[key_to_use] = value
        print(active_dict_to_add_to_list)
        # I know the dict should be 4 in length s I can realize I need to add here, but I don't like that...
        if len(active_dict_to_add_to_list) == 4:
            list_of_submissions.append(active_dict_to_add_to_list)
            # print('added', active_dict_to_add_to_list)
            active_dict_to_add_to_list = dict()
            current_loop += 1
    else:
        # we need to move to new iteration
        # add the current active dict to the list of submissions
        list_of_submissions.append(active_dict_to_add_to_list)
        print('added', active_dict_to_add_to_list)
        # clear the active dict so it can be added again
        active_dict_to_add_to_list = dict()
        current_loop += 1

print(list_of_submissions)

答案
data = {'resident_payer,1': 'William Brown',
        'Term Fee,amount_paid,1': '1',
        'method,1': 'credit',
        'document_id,1': '1',

        'resident_payer,2': None,
        'Term Fee,amount_paid,2': '0',
        'method,2': 'other',
        'document_id,2': '0'}

out = {}
for k, v in data.items():
    out.setdefault(k.split(',')[-1], {})[k.split(',')[0]] = v

out = list(out.values())

print(out)

打印:

[{'resident_payer': 'William Brown', 'Term Fee': '1', 'method': 'credit', 'document_id': '1'}, {'resident_payer': None, 'Term Fee': '0', 'method': 'other', 'document_id': '0'}]
另一答案

喜欢这个:

l = [{f'resident_payer,{n}':data[f'resident_payer,{n}'],
      f'Term Fee,amount_paid,{n}':data[f'Term Fee,amount_paid,{n}'],
      f'method,{n}': data[f'method,{n}'],
      f'document_id,1': data[f'document_id,1']} for n in [1,2]]
另一答案

尝试一下,KeyError一旦用完所有索引就中断循环。

start_index, parsed_dict = 1, []

keys = ["resident_payer", "Term Fee,amount_paid",
        "Term Fee,amount_paid", "method", "document_id"]

while True:
    try:
        for key in keys:
            parsed_dict.append({key: data[key + "," + str(start_index)]})
    except KeyError:
        break

    start_index += 1

print(parsed_dict)

输出,

[{'resident_payer': 'William Brown'}, {'Term Fee,amount_paid': '1'}, {'Term Fee,amount_paid': '1'}, {'method': 'credit'}, {'document_id': '1'}, 
{'resident_payer': None}, {'Term Fee,amount_paid': '0'}, {'Term Fee,amount_paid': '0'}, {'method': 'other'}, {'document_id': '0'}]

以上是关于字典拆分/理解的主要内容,如果未能解决你的问题,请参考以下文章

将元素为字典的列拆分为多列[重复]

将元组字典拆分为熊猫数据框

将括号分隔的字符串拆分为字典

13 个非常有用的 Python 代码片段

将列表转换为 DataFrame 并在 DataFrame 列中拆分嵌套字典 - Python 3.6

为啥将拆分为 wav 文件的旋律转换为拆分的 mp3 会在片段边界处产生不好的声音?