0

I have a variable.

Score = {}

After some calculation, the value of Score is :

{
'449 22876': 0.7491997,
'3655 6388': 0.99840045,
'2530 14648': 0.9219989,
'19957 832': 0.9806836,
'2741 23293': 0.64072967,
'22324 7525': 0.986661,
'9090 3811': 0.90206504,
'10588 5352': 0.8018138,
'18231 7515': 0.9991332,
'17807 14648': 0.9131582
.....
}

I want to sort it by the third value(e.g. 0.7491997). I only want to get the top 100 high score. How can I do?

Adam Boinet
  • 521
  • 8
  • 22
  • Why does `score` have its key as the first two values separated by a space? are you sure you want that? – Mario Ishac Jun 04 '20 at 12:59
  • Ignoring *why* you'd want to do that, have a look here: [How do I sort a dictionary by value?](https://stackoverflow.com/a/613218/4644044) – Lorenz Leitner Jun 04 '20 at 13:02
  • Do you want a sorted dictionary with the top 100 values, or just the top 100 values in a list? – timgeb Jun 04 '20 at 13:12

1 Answers1

0

if you want to sort the dictionary by the values of the dictionary (which is what I am getting from your question) you could do it with this lambda function:

    sorted_dict = sorted(score.items(), key=lambda x: x[1])
rachelalll
  • 112
  • 1
  • 2
  • 5