找到至少两个词组中的公共密钥

时间:2017-10-05 05:37:55

标签: python

我想获得以下形式的词典(不严格):

{65: ['Fresh', 'Frozen'],
 66: ['Fresh', 'Delicatessen'],
 75: ['Grocery', 'Detergents_Paper'],
 128: ['Fresh', 'Delicatessen'],
 154: ['Milk', 'Grocery', 'Delicatessen']}

当我有一个输入是dicts列表。他们的钥匙可能是相交的,而不是价值观。例如,在我们的示例中,我们在两个值为65Fresh的dicts中具有相同的键Frozen

这是我的解决方案,我想改进它:

outliers = [
    {65: 'Fresh', 66: 'Fresh', 81: 'Fresh', 95: 'Fresh', 96: 'Fresh',
     128: 'Fresh', 171: 'Fresh', 193: 'Fresh', 218: 'Fresh', 304: 'Fresh',
     305: 'Fresh', 338: 'Fresh', 353: 'Fresh', 355: 'Fresh', 357: 'Fresh',
     412: 'Fresh'},

    {86: 'Milk', 98: 'Milk', 154: 'Milk', 356: 'Milk'},

    {75: 'Grocery', 154: 'Grocery'},

    {38: 'Frozen', 57: 'Frozen', 65: 'Frozen', 145: 'Frozen', 175: 'Frozen',

     264: 'Frozen', 325: 'Frozen', 420: 'Frozen', 429: 'Frozen', 439: 'Frozen'},

    {75: 'Detergents_Paper', 161: 'Detergents_Paper'},

    {66: 'Delicatessen', 109: 'Delicatessen', 128: 'Delicatessen',
     137: 'Delicatessen', 142: 'Delicatessen', 154: 'Delicatessen',
     183: 'Delicatessen', 184: 'Delicatessen', 187: 'Delicatessen',
     203: 'Delicatessen', 233: 'Delicatessen', 285: 'Delicatessen',
     289: 'Delicatessen', 343: 'Delicatessen'}
]

common_outliers = {}
for outlier in outliers:
    for idx, feature in outlier.items():
        if idx not in common_outliers:
            common_outliers[idx] = [feature]
        else:
            common_outliers[idx].append(feature)
common_outliers = {idx: features for idx, features in common_outliers.items()
                   if len(features) > 1}
print(common_outliers)

2 个答案:

答案 0 :(得分:1)

你也可以使用defaultdict来简化它,它可以摆脱关键检查步骤:

from collections import defaultdict

common_outliers = defaultdict(list)
for outlier in outliers:
    for idx, feature in outlier.items():
        common_outliers[idx].append(feature)

common_outliers = {idx: features for idx, features in common_outliers.items()
                    if len(features) > 1}

答案 1 :(得分:1)

from itertools import chain
from collections import defaultdict

d_dict = defaultdict(list)
for k, v in chain.from_iterable([ i.items() for i in outliers ]):
    d_dict[k].append(v)

dict(filter( lambda x:len(x[1])>1, d_dict.items() ))

输出:

{65: ['Fresh', 'Frozen'],
 66: ['Fresh', 'Delicatessen'],
 75: ['Grocery', 'Detergents_Paper'],
 128: ['Fresh', 'Delicatessen'],
 154: ['Milk', 'Grocery', 'Delicatessen']}