如何基于Python中其他键的值在字典列表中增加特定键
我有下面的字典清单如何基于Python中其他键的值在字典列表中增加特定键,python,Python,我有下面的字典清单 my_list = [ {'id': 0, 'au_type': 1, 'sequence_id': 0, 'AU_start_position': 0}, {'id': 0, 'au_type': 1, 'sequence_id': 0, 'AU_start_position': 4095}, {'id': 0, 'au_type': 1, 'sequence_id': 0, 'AU_start_position': 8092}, {'id': 0, 'au_type':
my_list = [
{'id': 0, 'au_type': 1, 'sequence_id': 0, 'AU_start_position': 0},
{'id': 0, 'au_type': 1, 'sequence_id': 0, 'AU_start_position': 4095},
{'id': 0, 'au_type': 1, 'sequence_id': 0, 'AU_start_position': 8092},
{'id': 0, 'au_type': 3, 'sequence_id': 0, 'AU_start_position': 5678},
{'id': 0, 'au_type': 1, 'sequence_id': 1, 'AU_start_position': 13525},
{'id': 0, 'au_type': 1, 'sequence_id': 1, 'AU_start_position': 13587},
{'id': 0, 'au_type': 1, 'sequence_id': 1, 'AU_start_position': 14576},
{'id': 0, 'au_type': 3, 'sequence_id': 1, 'AU_start_position': 15019},
{'id': 0, 'au_type': 3, 'sequence_id': 1, 'AU_start_position': 15560},
{'id': 0, 'au_type': 3, 'sequence_id': 1, 'AU_start_position': 16004}
]
我正在寻找一种方法来编辑具有相同'au\u type'
和'sequence\u id'
的元素的'id'值(现在总是0)。
'id'
应为'AU\u start\u position'
的不同值增加一。
因此,根据上面的列表,我的目标是得到以下列表:
my_new_list = [
{'id': 0, 'au_type': 1, 'sequence_id': 0, 'AU_start_position': 0},
{'id': 1, 'au_type': 1, 'sequence_id': 0, 'AU_start_position': 4095},
{'id': 2, 'au_type': 1, 'sequence_id': 0, 'AU_start_position': 8092},
{'id': 0, 'au_type': 3, 'sequence_id': 0, 'AU_start_position': 5678},
{'id': 0, 'au_type': 1, 'sequence_id': 1, 'AU_start_position': 13525},
{'id': 1, 'au_type': 1, 'sequence_id': 1, 'AU_start_position': 13587},
{'id': 2, 'au_type': 1, 'sequence_id': 1, 'AU_start_position': 14576},
{'id': 0, 'au_type': 3, 'sequence_id': 1, 'AU_start_position': 15019},
{'id': 1, 'au_type': 3, 'sequence_id': 1, 'AU_start_position': 15560},
{'id': 2, 'au_type': 3, 'sequence_id': 1, 'AU_start_position': 16004} ]
感谢您提供的任何提示我不知道这个问题是否存在更干净的解决方案,但一种可能的方法是创建另一个字典,其中键为au_type和sequence_id。值为id 例如,让“控制”这个辅助字典。对于描述中的第一个输入,代码将验证控件[au_type][sequence_id]是否存在,以及此id的值是多少。以下步骤将增加值o id并将au_start_位置添加到集合中。结构如下所示:
{
'au_type': {
'sequence_id': {
id: 0,
AU_start_position: set()
}
}
}
也许这是一种过分的做法,但可能会奏效。您可以使用
iterrools.groupby
对所有具有相同au\u类型和sequence\u id
的dict进行分组,然后更新每个dict的id
>>> from itertools import groupby
>>> res = [dict(d, id=i) for _,grp in groupby(my_list, key=lambda d: (d['au_type'],d['sequence_id'])) for i,d in enumerate(grp)]
>>>
>>> print (*res, sep='\n')
{'id': 0, 'au_type': 1, 'sequence_id': 0, 'AU_start_position': 0}
{'id': 1, 'au_type': 1, 'sequence_id': 0, 'AU_start_position': 4095}
{'id': 2, 'au_type': 1, 'sequence_id': 0, 'AU_start_position': 8092}
{'id': 0, 'au_type': 3, 'sequence_id': 0, 'AU_start_position': 5678}
{'id': 0, 'au_type': 1, 'sequence_id': 1, 'AU_start_position': 13525}
{'id': 1, 'au_type': 1, 'sequence_id': 1, 'AU_start_position': 13587}
{'id': 2, 'au_type': 1, 'sequence_id': 1, 'AU_start_position': 14576}
{'id': 0, 'au_type': 3, 'sequence_id': 1, 'AU_start_position': 13525}
{'id': 1, 'au_type': 3, 'sequence_id': 1, 'AU_start_position': 13587}
{'id': 2, 'au_type': 3, 'sequence_id': 1, 'AU_start_position': 14576}
你能详细说明你想解决什么样的问题吗?也许有更好的办法。