I've been trying to find the largest result in a list - using the confidence value.
Examples of the lists:
[[{u'categories': [u'health-beauty'], u'confidence': 0.3333333333333333},
{u'categories': [u'activities-events'], u'confidence': 0.6666666666666666}]]
Would return the activities-events dictionary
[[{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}]]
Would return all three as they are equal
[[{u'categories': [u'entertainment'], u'confidence': 1.0}]]
Would return entertainment
I tried to utilise python's max function:
seq = [x['confidence'] for x in d[0]]
max(seq)
but that just returns the value
You can find the maximum confidence as in your own example, then use filter
to create a list of all the maximum records:
max_conf = max(x['confidence'] for x in d[0])
filter(lambda x: x['confidence']==max_conf, d[0])
As noted in the comment below, filter
could be replaced with list comprehension:
max_records = [x for x in d[0] if x['confidence'] == max_conf]
max(d[0], key=lambda x: x['confidence'])
returns the whole element from d[0]
with the highest confidence
attribute.
Another way:
import operator as op
max(d[0], key=op.attrgetter('confidence'))
sorted(d[0], key=lambda k: k['confidence'])[-1]
Just one more approach. Also returns the whole element from d[0]
with the highest confidence
attribute.
If you want to retrieve all the matches with highest confidence, max
is not the option. You first need to sort it by key = confidence (you can use sorted for the purpose, and operator.itemgetter to retrieve the key) and then group the elements (you can use itertools.groupby ) based on the confidence. Finally return the group with the highest confidence
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]]
Examples
>>> 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']]
>>>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.