仅使用标题很难解释这个问题。
我有很多字典,dict_list,长约18k。它们每个上的键之一是“过程”。流程为“蚀刻”和“ Depo”,每个流程将重复一小段,然后更改为另一个,然后返回。这些称为“行程”。
我需要将类似的进程分组到一个列表中,直到该进程更改为止,然后将该列表插入“运行”字典中。这是一个更好的视觉解释:
dict_list = [{"PROCESS": "Etch"},{"PROCESS": "Etch"},{"PROCESS": "Etch"},
{"PROCESS": "Etch"},{"PROCESS": "Etch"},{"PROCESS": "Etch"},
{"PROCESS": "Etch"},{"PROCESS": "Etch"},{"PROCESS": "Etch"},
{"PROCESS": "Etch"},{"PROCESS": "Etch"},{"PROCESS": "Etch"},
{"PROCESS": "Etch"},{"PROCESS": "Etch"},{"PROCESS": "Etch"},
{"PROCESS": "Depo"},{"PROCESS": "Depo"},{"PROCESS": "Depo"},
{"PROCESS": "Depo"},{"PROCESS": "Depo"},{"PROCESS": "Depo"},
{"PROCESS": "Depo"},{"PROCESS": "Depo"},{"PROCESS": "Depo"},
{"PROCESS": "Depo"},{"PROCESS": "Depo"},{"PROCESS": "Depo"},
{"PROCESS": "Depo"},{"PROCESS": "Depo"},{"PROCESS": "Depo"},
{"PROCESS": "Etch"},{"PROCESS": "Etch"},{"PROCESS": "Etch"},
{"PROCESS": "Etch"},{"PROCESS": "Etch"}]
基本上,如果我遍历dict_list
,一行一行地打印每个“ PROCESS”,则看起来像这样:
>>"Etch"
>>"Etch"
>>"Etch"
>>"Etch"
>>"Depo"
>>"Depo"
>>"Depo"
>>"Depo"
>>"Etch"
>>"Etch"
>>"Etch"
>>"Etch"
>>"Depo"
>>"Depo"
>>"Depo"
>>"Depo"
对于该示例,我将有4个“运行”词典,每个词典包含4个词典的列表。
我需要将它们分组到列表中,然后再分类为字典,例如:
new_dict_list = {
"run 1": [{"PROCESS": "Etch"},{"PROCESS": "Etch"},{"PROCESS": "Etch"},{"PROCESS": "Etch"},{"PROCESS": "Etch"},{"PROCESS": "Etch"},{"PROCESS": "Etch"},{"PROCESS": "Etch"},{"PROCESS": "Etch"},{"PROCESS": "Etch"},{"PROCESS": "Etch"},{"PROCESS": "Etch"},{"PROCESS": "Etch"},{"PROCESS": "Etch"},{"PROCESS": "Etch"}],
"run 2": [{"PROCESS": "Depo"},{"PROCESS": "Depo"},{"PROCESS": "Depo"},{"PROCESS": "Depo"},{"PROCESS": "Depo"},{"PROCESS": "Depo"},{"PROCESS": "Depo"},{"PROCESS": "Depo"},{"PROCESS": "Depo"},{"PROCESS": "Depo"},{"PROCESS": "Depo"},{"PROCESS": "Depo"},{"PROCESS": "Depo"},{"PROCESS": "Depo"},{"PROCESS": "Depo"}],
"run 3": [{"PROCESS": "Etch"},{"PROCESS": "Etch"},{"PROCESS": "Etch"},{"PROCESS": "Etch"},{"PROCESS": "Etch"}]
}
那会是这样的:
遍历每本字典
将第一个词典放在列表中, 然后将该列表添加到新词典中(我们称之为运行)
在下一次迭代中,如果dictionary [“ PROCESS”]相同,则将其存储 进入同一列表和同一词典
如果dictionary [“ PROCESS”]更改,请将当前词典存储在 新列表,然后放入新词典
我只是不确定如何将其放入python逻辑中。我对此还很陌生。
这是我到目前为止所拥有的:
prev_process = ""
counter = 0
new_dict_list = {}
for dictionary in dict_list:
if dictionary["PROCESS"] != prev_process:
counter += 1
prev_process = dictionary["PROCESS"]
new_dict_list["run " + counter] = dictionary
我感觉那里应该有一个while循环,“ while dictionary [“ PROCESS”]保持不变,做一些事情”,但是我不知道如何将其放入python中,或者如何分解(因为如果我像现在这样检查它,该条件将始终为真。
答案 0 :(得分:4)
您可以使用itertools.groupby
:
import itertools
dict_list = [{"PROCESS": "Etch"},{"PROCESS": "Etch"},{"PROCESS": "Etch"},{"PROCESS": "Etch"},{"PROCESS": "Etch"},{"PROCESS": "Etch"},{"PROCESS": "Etch"},{"PROCESS": "Etch"},{"PROCESS": "Etch"},{"PROCESS": "Etch"},{"PROCESS": "Etch"},{"PROCESS": "Etch"},{"PROCESS": "Etch"},{"PROCESS": "Etch"},{"PROCESS": "Etch"},{"PROCESS": "Depo"},{"PROCESS": "Depo"},{"PROCESS": "Depo"},{"PROCESS": "Depo"},{"PROCESS": "Depo"},{"PROCESS": "Depo"},{"PROCESS": "Depo"},{"PROCESS": "Depo"},{"PROCESS": "Depo"},{"PROCESS": "Depo"},{"PROCESS": "Depo"},{"PROCESS": "Depo"},{"PROCESS": "Depo"},{"PROCESS": "Depo"},{"PROCESS": "Depo"},{"PROCESS": "Etch"},{"PROCESS": "Etch"},{"PROCESS": "Etch"},{"PROCESS": "Etch"},{"PROCESS": "Etch"}]
new_d = {'run {}'.format(i):list(b) for i, [_, b] in enumerate(itertools.groupby(dict_list, key=lambda x:x["PROCESS"]), 1)}
输出:
{'run 1': [{'PROCESS': 'Etch'}, {'PROCESS': 'Etch'}, {'PROCESS': 'Etch'}, {'PROCESS': 'Etch'}, {'PROCESS': 'Etch'}, {'PROCESS': 'Etch'}, {'PROCESS': 'Etch'}, {'PROCESS': 'Etch'}, {'PROCESS': 'Etch'}, {'PROCESS': 'Etch'}, {'PROCESS': 'Etch'}, {'PROCESS': 'Etch'}, {'PROCESS': 'Etch'}, {'PROCESS': 'Etch'}, {'PROCESS': 'Etch'}],
'run 2': [{'PROCESS': 'Depo'}, {'PROCESS': 'Depo'}, {'PROCESS': 'Depo'}, {'PROCESS': 'Depo'}, {'PROCESS': 'Depo'}, {'PROCESS': 'Depo'}, {'PROCESS': 'Depo'}, {'PROCESS': 'Depo'}, {'PROCESS': 'Depo'}, {'PROCESS': 'Depo'}, {'PROCESS': 'Depo'}, {'PROCESS': 'Depo'}, {'PROCESS': 'Depo'}, {'PROCESS': 'Depo'}, {'PROCESS': 'Depo'}],
'run 3': [{'PROCESS': 'Etch'}, {'PROCESS': 'Etch'}, {'PROCESS': 'Etch'}, {'PROCESS': 'Etch'}, {'PROCESS': 'Etch'}]
}
itertools.groupby
根据单个键对数据进行分类。在这种情况下,数据将按'PROCESS'
键的值分组,从而导致嵌套列表(包含键值)和所有具有匹配键值的元素。要创建自定义的'run {number}'
键,请使用enumerate
以一种简洁的方式跟踪当前的迭代索引。