2

All,

I am struggling with sorting a dictionary containing multiple values for each key by a single key. I have the following dictionary

{ 
     'time': [2, 1, 3], 
     'x_coordinates': [3, 5, 4], 
     'y_coordinates': [6, 7, 8]
}

and desire the following as an output:

{
     'time': [1, 2, 3], 
     'x_coordinates': [5, 3, 4], 
     'y_coordinates': [7, 6, 8]
}

How can I achieve this in the most efficient way possible? I have tried following the internet for suggestions, but there is nothing about sorting multi-value keys by a single key. Any help will be appreciated.

DarkDrassher34
  • 69
  • 3
  • 15
  • Note my keys contain 68,874 values and more than 20 different keys, so computational speed is of concern. – DarkDrassher34 Feb 27 '20 at 13:48
  • 2
    Does this answer your question? [How do I sort a dictionary by value?](https://stackoverflow.com/questions/613183/how-do-i-sort-a-dictionary-by-value) – KJTHoward Feb 27 '20 at 13:49
  • No. I tried that already, seems to not work for keys which are not ints. Try it :) – DarkDrassher34 Feb 27 '20 at 13:52
  • this is a little unclear, you have a single dictionary and you want to sort the internal values? whats the sorting logic? `time` seems to be small to large but the other 2 are different – Nullman Feb 27 '20 at 13:53
  • Yes. Sort all values by time. That way the x and y coordinates correspond to the time ordering. – DarkDrassher34 Feb 27 '20 at 13:54
  • ahh, i think i understand, you want to do [argsort](https://stackoverflow.com/a/57540097/7540911), that is to say you want to sort time and using that permutation sort the rest of you items – Nullman Feb 27 '20 at 14:01

2 Answers2

5
d = {'time': [2, 1, 3], 'x_coordinates': [3, 5, 4], 'y_coordinates': [6, 7, 8]}

key = 'time'
index = [f for f, _ in sorted(enumerate(d[key]), key=lambda x : x[1])]
d = {k : [v[i] for i in index] for k, v in d.items()}

output:

{'time': [1, 2, 3], 'x_coordinates': [5, 3, 4], 'y_coordinates': [7, 6, 8]}
tomgalpin
  • 1,943
  • 1
  • 4
  • 12
kederrac
  • 16,819
  • 6
  • 32
  • 55
0

It seems like in your program 'time' and 'position' are correlated. I think it will be easier for you to use a dataframe where each key of the dictionary corresponds to a variable, that way when you sort it by, for example, 'time', all the information regarding time will move to the first position and so on.

import pandas as pd

info=pd.DataFrame({'time': [2, 1, 3], 'x_coordinates': [3, 5, 4], 'y_coordinates': [6, 7, 8]})
info.sort_values(by='time', axis=0)

Hope it helps.