0

I want to receive input as a dictionary, and rearrange the key values in ascending order without changing the keys.

Ex: Input:

{"Hi:5, "Hey":3, "Hello":10}

Output

{"Hi":3, "Hey":5, "Hello":10}

2 Answers2

0

if you only want to change the order of the values of the keys here is some simple code

example = {"Hi":5, "Hey":3, "Hello":10} #Your dictionary
VALUES = [] #Here the values of the keys will be stored
#Get all the values of the dict keys
for item in example:
    value = example[item]
    VALUES.append(value)

VALUES.sort() #Sorts them from low number to high

i=0
for item in example:
    example[item] = VALUES[i]
    i +=1

This would ouput the following:

{'Hi': 3, 'Hey': 5, 'Hello': 10}
Countour-Integral
  • 1,138
  • 2
  • 7
  • 21
0

you could unpack it into lists then sort the list with values and then repack like this:

stuff = {"Hi":5, "Hey":3, "Hello":10}
keys = []
values = []

for key in stuff.keys():
    keys.append(key)
    values.append(stuff[key])

values.sort()

i = 0
for key in stuff.keys():
    stuff[key] = values[i]
    i += 1

print stuff