按值将python字典中的键/值对分组,并保持原始键关联

时间:2018-09-24 16:54:13

标签: python dictionary grouping

正在寻找一种有效的方法来按字典的相似度对字典中的值进行分组,从而导致将原始关键字分组为列表的反向字典。我可以想到执行此操作的笨拙方法,但对其他方法也很感兴趣。

假设我的字典如下:

{'d1': {'goober': True, 'horse': 42},
 'd2': {'goober': True, 'horse': 42},
 'd3': {'goober': False, 'horse': 71}}

我想要的字典可能看起来像这样:

{'group1': {'dict': {'goober': True, 'horse': 42}, 'keys': ['d1', 'd2']},
 'group2': {'dict': {'goober': False, 'horse': 71}, 'keys': ['d3']}}

重要的细节是,原始键d1d2已被分组在任意命名的group1下,并由原始词典中的相同字典进行分组。保持原始密钥(存储在d1列表中的d2keys)以及与它们关联的词典也很重要。 d3是一个人,因为它的字典是唯一的。

我的更大目标是制作一个字典字典,其中包含用于其他目的的参数,但是在参数相同的地方将它们分组,这样我可以更高效地一起运行它们。

任何提示或建议将不胜感激!

3 个答案:

答案 0 :(得分:2)

您可以使用itertools.groupby

d = {'d1': {'goober': True, 'horse': 42},
     'd2': {'goober': True, 'horse': 42},
     'd3': {'goober': False, 'horse': 71}}
from itertools import groupby
from operator import itemgetter
l = [{'dict': k, 'keys': list(map(itemgetter(0), g))} for k, g in groupby(sorted(d.items(), key=lambda t: t[1].items()), itemgetter(1))]

使l变为:

[{'dict': {'goober': True, 'horse': 42}, 'keys': ['d1', 'd2']}, {'dict': {'goober': False, 'horse': 71}, 'keys': ['d3']}]

答案 1 :(得分:1)

这是可能的,但是对于常规Python来说很乏味。如果您愿意使用第三方库,则可以使用Pandas执行GroupBy聚合:

import pandas as pd

d = {'d1': {'goober': True, 'horse': 42},
     'd2': {'goober': True, 'horse': 42},
     'd3': {'goober': False, 'horse': 71}}

df = pd.DataFrame(d).T.reset_index().groupby('goober')\
                    .agg(lambda x: set(x)).reset_index()

print(df)

#   goober     index horse
# 0  False      {d3}  {71}
# 1   True  {d1, d2}  {42}

res = [{'dict': {'goober': row.goober, 'horse': next(iter(row.horse))},
        'keys': list(row.index)} for row in df.itertuples(index=False)]

结果:

[{'dict': {'goober': False, 'horse': 71}, 'keys': ['d3']},
 {'dict': {'goober': True, 'horse': 42}, 'keys': ['d1', 'd2']}]

请注意,我尚未应用任何逻辑来派生'group1''group2'外字典键。尚不清楚如何。如果它们是任意标识符,则可以在后续步骤中使用enumerate

res2 = {f'group{idx}': d for idx, d in enumerate(res, 1)}

结果:

{'group1': {'dict': {'goober': False, 'horse': 71}, 'keys': ['d3']},
 'group2': {'dict': {'goober': True, 'horse': 42}, 'keys': ['d1', 'd2']}}

答案 2 :(得分:1)

这就是我要做的

cat bitnami_application_password