I am trying to sort a multi-tuple with double indexes in Python. I want to sort it by it's second index. The tuple output, which is the output of "print(max_values)", looks like:
(array([[4.15498641]]), 1)
(array([[2.31940546]]), 4)
(array([[0.96185454]]), 8)
(array([[1.29915758]]), 11)
(array([[1.66805024]]), 5)
(array([[1.25312376]]), 13)
(array([[1.81367542]]), 7)
(array([[3.16895748]]), 14)
(array([[3.74632224]]), 0)
(array([[4.87073571]]), 10)
(array([[1.8860763]]), 12)
(array([[1.25379793]]), 6)
(array([[0.60556452]]), 15)
(array([[3.09510515]]), 3)
(array([[2.7700944]]), 9)
(array([[2.65579492]]), 2)
What I want to do exactly is, this is to put these tuples in order according to the second index of the tuples you see.
For example, in the output I added, the second index of the first tuple is 1, while the second index of the second tuple is 4. What I want to do is set the second index equal to 2.
I am also adding the full code, "takeSecond" function didn't work.
import numpy as np
import random
random.seed(2)
np.random.seed(2)
x_train = [(np.random.randn(1,3),0), (np.random.randn(1,3),1), (np.random.randn(1,3),2) , (np.random.randn(1,3),3),
(np.random.randn(1,3),4), (np.random.randn(1,3),5), (np.random.randn(1,3),6) , (np.random.randn(1,3),7),
(np.random.randn(1,3),8), (np.random.randn(1,3),9), (np.random.randn(1,3),10), (np.random.randn(1,3),11),
(np.random.randn(1,3),12),(np.random.randn(1,3),13),(np.random.randn(1,3),14), (np.random.randn(1,3),15)]
neurons = [(np.random.randn(3,1),0), (np.random.randn(3,1),1), (np.random.randn(3,1),2),
(np.random.randn(3,1),3), (np.random.randn(3,1),4), (np.random.randn(3,1),5),
(np.random.randn(3,1),6), (np.random.randn(3,1),7), (np.random.randn(3,1),8)]
def takeSecond(elem):
return elem[1]
#%%
random.shuffle(x_train)
for i in range (len(x_train)):
results = []
winning_neurons = []
for j in range (len(neurons)):
result = np.dot(x_train[i][0],neurons[j][0])
results.append((result,x_train[i][1]))
#results.sort(key=takeSecond)
#print(results)
max_values = max(results)
print(max_values)
max_index = results.index(max_values)
winning_neurons.append(max_index)
#print(winning_neurons)
Other tuple sorting functions on the internet, didn't work and gave errors like "index error" or "sort() function not working for tuples".
Could you help me to sort this? Thank you in advance.
CodePudding user response:
This may be what you want:
out = sorted(max_values, key=lambda x: x[1])
Output:
[(array([[3.74632224]]), 0),
(array([[4.15498641]]), 1),
(array([[2.65579492]]), 2),
(array([[3.09510515]]), 3),
(array([[2.31940546]]), 4),
(array([[1.66805024]]), 5),
(array([[1.25379793]]), 6),
(array([[1.81367542]]), 7),
(array([[0.96185454]]), 8),
(array([[2.7700944]]), 9),
(array([[4.87073571]]), 10),
(array([[1.29915758]]), 11),
(array([[1.8860763]]), 12),
(array([[1.25312376]]), 13),
(array([[3.16895748]]), 14),
(array([[0.60556452]]), 15)]
CodePudding user response:
You need to actually use takeSecond
as a key
function to get the behavior you want. results.sort(key=takeSecond)
does sort the list, but that order has no effect on the call to max()
, and you make no use of the sorted results
list anywhere else.
By default, the max
method takes the first item in a tuple for comparison, hence you need to pass a key
here too:
max_values = max(results, key=takeSecond)
In your sample data, this would return (array([[0.60556452]]), 15)
.
If you just need the max tuple based on this criteria, using max()
is better than sorting the entire results
list. If you need to have sorted results
anyway, then there is no need to use max()
, the maximum tuple will be at results[-1]
, the last item since results.sort(key=takeSecond)
will be sorted in ascending order.