Reputation: 45
I have two sets of data in the below code that are linked - 5 'networks', and 5 singular results through 'TT' values. Each TT value corresponds to a network set (e.g. TT1=100 is a result from network1).
My goal is to be able to rank the 5 networks based on the TT results.
network1 = [0,1,0,0,0,0,1,0,0,0,1,1,0,0,1]
network2 = [0,1,0,0,0,0,0,0,1,0,1,1,0,0,1]
network3 = [0,1,0,0,0,0,0,1,0,0,1,1,0,0,1]
network4 = [0,1,0,0,0,0,0,0,0,0,1,1,0,1,1]
network5 = [0,1,1,0,0,0,0,0,0,0,1,1,0,0,1]
networks = [network1, network2, network3, network4, network5]
TT1 = 100
TT2 = 70
TT3 = 80
TT4 = 105
TT5 = 120
TTranks = [TT1,TT2,TT3,TT4,TT5]
s = {x: i for i, x in enumerate(sorted(set(TTranks)))}
ranks = [s[x] for x in TTranks]
print(ranks)
The results below from this process rank the TT values only, although I am unsure how to link it so that the corresponding networks are also ranked appropriately.
[2, 0, 1, 3, 4]
That is, I would like it to then rank the 5 networks as network2, network3, network1, network4, network5. This will be very useful as I would like to be able to call from the ordered network results later on.
Additionally, is there a more effective way of completing this process?
I appreciate all the help!!
Upvotes: 2
Views: 58
Reputation: 15505
You can use the key
optional argument of the sort
/sorted
functions to sort according to a custom value.
network1 = [0,1,0,0,0,0,1,0,0,0,1,1,0,0,1]
network2 = [0,1,0,0,0,0,0,0,1,0,1,1,0,0,1]
network3 = [0,1,0,0,0,0,0,1,0,0,1,1,0,0,1]
network4 = [0,1,0,0,0,0,0,0,0,0,1,1,0,1,1]
network5 = [0,1,1,0,0,0,0,0,0,0,1,1,0,0,1]
networks = [network1, network2, network3, network4, network5]
TT1 = 100
TT2 = 70
TT3 = 80
TT4 = 105
TT5 = 120
TT = [TT1, TT2, TT3, TT4, TT5]
sorted_networks_with_indices = sorted(enumerate(networks), key=lambda t: TT[t[0]])
sorted_indices = [i for i,n in sorted_networks_with_indices]
sorted_networks = [n for i,n in sorted_networks_with_indices]
print(sorted_indices)
print(sorted_networks)
[1, 2, 0, 3, 4]
[[0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1],
[0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1],
[0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1],
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1],
[0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1]]
enumerate(networks)
lists the pairs i,n
where i
is the index and n
the network. These pairs are sorted with the sorted
function, and the key used to sort is lambda t: TT[t[0]]
. Here t
is the pair i,n
, so t[0]
is the index i
. Therefore TT[t[0]]
is the TT corresponding to index-network pair t
.
The indices in sorted_indices
are [1, 2, 0, 3, 4]
and not [2, 3, 1, 4, 5]
because python lists are 0-indexed, but this corresponds to network2, network3, network1, network4, network5. If you are not happy with that you can replace the relevant line with:
sorted_indices = [i+1 for i,n in sorted_networks_with_indices]
sorted
function and its key
argument: https://docs.python.org/3/library/functions.html#sortedsort
member of list
and its key
argument: https://docs.python.org/3/library/stdtypes.html#list.sortUpvotes: 1
Reputation: 3746
Could you please explain how networks and TTranks are connected? The code shows no dependency on these two.
You simply want the indices for sorting TTranks? Then you should avoid set(TTranks), since the order will not be preserved in a set, also duplicates will be deleted. There are several ways to get the sorting indices, I prefer argsort from numpy:
import numpy as np
print(np.argsort(TTranks))
# [1 2 0 3 4]
Upvotes: 0