python - 在字典中找到最相关的 child

标签 python categories

拿这本字典:

{'local': {'count': 7,
    'dining-and-nightlife': {'count': 1,
        'bar-clubs': {'count': 1}
    },
    'activities-events': {'count': 6,
        'outdoor-adventures': {'count': 4},
        'life-skill-classes': {'count': 2}
    }
}}

如何确定最相关的匹配项(在 30% 的余地内)?例如,activities-events 的计数为 6,因此 6/7 = 85%,其子 outdoor-adventures 的计数为 4 out 6 (66%)。因此,与此最相关的类别是户外探险。

在这个例子中:

{'local': {'count': 11,
    'dining-and-nightlife': {'count': 4,
        'bar-clubs': {'count': 4}
    },
    'activities-events': {'count': 6,
        'outdoor-adventures': {'count': 4},
        'life-skill-classes': {'count': 2}
    }
}}

餐饮和夜生活 (33%) 与酒吧俱乐部 (100%) 和事件 (54%) 户外探险 (66%)。

我希望由

确定百分比截止值
cutoff = 0.3

这里的想法是确定哪个类别最相关,移除较小的结果(低于 30%)匹配。

@F.J 在下面回答了这个问题,但现在我想更新树中的计数。

初始输出:

{'local': {'activities-events': {'count': 6,
                             'life-skill-classes': {'count': 2},
                             'outdoor-adventures': {'count': 4}},
       'count': 11,
       'dining-and-nightlife': {'bar-clubs': {'count': 4}, 'count': 4}}}

后期输出:

{'local': {'activities-events': {'count': 6,
                             'life-skill-classes': {'count': 2},
                             'outdoor-adventures': {'count': 4}},
       'count': 10,
       'dining-and-nightlife': {'bar-clubs': {'count': 4}, 'count': 4}}}

最佳答案

以下应该可以工作,请注意,这将修改您的输入字典:

def keep_most_relevant(d, cutoff=0.3):
    for k, v in list(d.items()):
        if k == 'count':
            continue
        if 'count' in d and v['count'] < d['count'] * cutoff:
            del d[k]
        else:
            keep_most_relevant(v)

例子:

>>> d1 = {'local': {'count': 7, 'dining-and-nightlife': {'count': 1, 'bar-clubs': {'count': 1}}, 'activities-events': {'count': 6, 'outdoor-adventures': {'count': 4}, 'life-skill-classes': {'count': 2}}}}
>>> keep_most_relevant(d1)
>>> pprint.pprint(d1)
{'local': {'activities-events': {'count': 6,
                                 'life-skill-classes': {'count': 2},
                                 'outdoor-adventures': {'count': 4}},
           'count': 7}}

>>> d2 = {'local': {'count': 11, 'dining-and-nightlife': {'count': 4, 'bar-clubs': {'count': 4}}, 'activities-events': {'count': 6, 'outdoor-adventures': {'count': 4}, 'life-skill-classes': {'count': 2}}}}
>>> keep_most_relevant(d2)
>>> pprint.pprint(d2)
{'local': {'activities-events': {'count': 6,
                                 'life-skill-classes': {'count': 2},
                                 'outdoor-adventures': {'count': 4}},
           'count': 11,
           'dining-and-nightlife': {'bar-clubs': {'count': 4}, 'count': 4}}}

关于python - 在字典中找到最相关的 child ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12830102/

相关文章:

python - 自 apache arrow 1.0.1 发布以来用于长期存储的 Feather 格式

python - 如何按同月对这个字典列表进行分组? (后续)

python - 将估计器重置为先前的值 scikit learn

testing - 如何在没有混合测试的情况下运行按同一命名空间中的类别排序的 nunit 测试?

python - 如何在python上使用sagemath?

Python Pandas,创建指定列 dtypes 的空 DataFrame

mysql - WordPress 自定义查询

php - 在网页侧边栏显示类别列表

javascript - 对 highcharts 中的类别中的后代数据进行排序

magento - 添加启用所见即所得的类别属性