【问题标题】:Find largest match in list查找列表中最大的匹配项
【发布时间】:2012-10-31 16:20:02
【问题描述】:

我一直在尝试在列表中找到最大的结果 - 使用置信度值。

列表示例:

[[{u'categories': [u'health-beauty'], u'confidence': 0.3333333333333333},
 {u'categories': [u'activities-events'], u'confidence': 0.6666666666666666}]]

将返回 activities-events 字典

[[{u'categories': [u'home-garden'], u'confidence': 0.3333333333333333},
 {u'categories': [u'None of These'], u'confidence': 0.3333333333333333},
 {u'categories': [u'toys-kids-baby'], u'confidence': 0.3333333333333333}]]

将返回所有三个,因为它们是相等的

[[{u'categories': [u'entertainment'], u'confidence': 1.0}]]

会返回娱乐

我尝试利用python的max函数:

seq = [x['confidence'] for x in d[0]]
max(seq)

但这只是返回值

【问题讨论】:

  • '最大的结果'使用什么规则?
  • 更新了问题。谢谢@Tichodroma 会继续这样做。
  • 问题和你想要的应该很清楚。

标签: python list max min dictionary


【解决方案1】:
max(d[0], key=lambda x: x['confidence'])

d[0] 返回具有最高confidence 属性的整个元素。

另一种方式:

import operator as op

max(d[0], key=op.attrgetter('confidence'))

【讨论】:

  • 真的希望它可以返回 0.3333 示例中的所有三个。但这会做。谢谢。
【解决方案2】:
sorted(d[0], key=lambda k: k['confidence'])[-1]

只是另一种方法。还返回 d[0] 中具有最高 confidence 属性的整个元素。

【讨论】:

    【解决方案3】:

    您可以像自己的示例一样找到最大置信度,然后使用filter 创建所有最大记录的列表:

    max_conf = max(x['confidence'] for x in d[0])
    filter(lambda x: x['confidence']==max_conf, d[0])
    

    如以下评论中所述,filter 可以替换为列表理解:

    max_records = [x for x in d[0] if x['confidence'] == max_conf]
    

    【讨论】:

    • 你的意思可能是:max_conf = max(x['confidence'] for x in d[0]); result = [x for x in d[0] if x['confidence'] == max_conf]
    • 不,我的意思是使用过滤器功能,虽然我发现我的参数有误,所以我会更正它。当然,列表推导是另一种方式。
    • 仍然不正确:max(d[0], key=lambda x: x['confidence']) 返回整个字典,而不仅仅是'confidence' 部分。
    【解决方案4】:

    如果您想以最高的置信度检索所有匹配项,max 不是选项。您首先需要按 key = confidence 对其进行排序(您可以使用sorted 来达到目的,operator.itemgetter 来检索密钥)然后根据置信度对元素进行分组(您可以使用itertools.groupby)。最后返回置信度最高的组

    from itertools import groupby
    from operator import itemgetter
    groups = groupby(sorted(inlist[0], key = itemgetter(u'confidence'), reverse = True),
                     key = itemgetter(u'confidence'))
    [e[u'categories'] for e in next(groups)[-1]]
    

    例子

    >>> inlist = [[{u'categories': [u'health-beauty'], u'confidence': 0.3333333333333333}, {u'categories': [u'activities-events'], u'confidence': 0.6666666666666666}]]
    >>> groups = groupby(sorted(inlist[0], key = operator.itemgetter(u'confidence'), reverse = True),key = operator.itemgetter(u'confidence'))
    >>> [e[u'categories'] for e in next(groups)[-1]]
    [[u'activities-events']]
    >>> inlist = [[{u'categories': [u'home-garden'], u'confidence': 0.3333333333333333}, {u'categories': [u'None of These'], u'confidence': 0.3333333333333333}, {u'categories': [u'toys-kids-baby'], u'confidence': 0.3333333333333333}]]
    >>> groups = groupby(sorted(inlist[0], key = operator.itemgetter(u'confidence'), reverse = True),key = operator.itemgetter(u'confidence'))
    >>> [e[u'categories'] for e in next(groups)[-1]]
    [[u'home-garden'], [u'None of These'], [u'toys-kids-baby']]
    >>> inlist = [[{u'categories': [u'entertainment'], u'confidence': 1.0}]]
    >>> groups = groupby(sorted(inlist[0], key = operator.itemgetter(u'confidence'), reverse = True),key = operator.itemgetter(u'confidence'))
    >>> [e[u'categories'] for e in next(groups)[-1]]
    [[u'entertainment']]
    >>> 
    

    【讨论】:

      猜你喜欢
      • 2013-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-23
      • 2015-05-29
      • 2020-07-31
      • 1970-01-01
      相关资源
      最近更新 更多