Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sorting in python

Tags:

python

regex

I have a hashmap like so:

results[tweet_id] = {"score" : float(dot(query,doc) / (norm(query) * norm(doc))), "tweet" : tweet}

What I'd like to do is to sort results by the innser "score" key. I don't know how possible this is, I saw many sorting tutorials but they were for simple (not nested) data structures.

like image 216
tipu Avatar asked Nov 26 '25 21:11

tipu


1 Answers

>>> results=[{"s":1,"score":100},{"s":2,"score":101},{"s":3,"score":99},{"s":4,"score":1},{"s":5,"score":1000}]

>>> from operator import itemgetter

>>> sorted(results, key=itemgetter("score"))
[{'s': 4, 'score': 1}, {'s': 3, 'score': 99}, {'s': 1, 'score': 100}, {'s': 2, 'score': 101}, {'s': 5, 'score': 1000}]
like image 111
YOU Avatar answered Nov 28 '25 11:11

YOU



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!