Skip to content
Advertisement

Convert string list values of dictionary into ranked integers, but with their ranks + 1 and descending

Additionally, if the original dictionary has only one value, it gets a score of 1. For example, a dict that looks like this:

JavaScript

Would become the following:

JavaScript

This is a start for a dictionary with 2 keys, not including the 1-value situation:

JavaScript

That’s what I want for the values, now I just need to set the keys of a new dictionary to the keys of the original one, and assign the values from new_values.

Advertisement

Answer

Here is a solution, using range to rank the elements

JavaScript

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement