I have a problem. In my code I have the following dictionary:
dict = {} dict['A'] = {'slope': -51, 'score': 0} dict['B'] = {'slope': 12, 'score': 0} dict['C'] = {'slope': -4, 'score': 0} dict['D'] = {'slope': -31, 'score': 0} target = -21 The dictionary isn't sorted! Now what I am trying to do is, give each item in the dict a score (1 to 4). The dict closest to the target gets 4 points, the next one 3 points, etc.
First I thought I iterate over the dict values, but then I realised that I don't know which if there are values closer than the current one, so I can't keep the score about that.
In the end, it needs to look like this:
{'A': {'slope': -51, 'score': 2}, 'B': {'slope': 12, 'score': 1}, 'C': {'slope': -4, 'score': 3}, 'D': {'slope': -31, 'score': 4}} What can I do to achieve this?