Hi, I want to sort a tuple consisting of two floats and one array by the absolute value of the first float in reverse order. What I did is the following:

from numpy import *

test = [(-0.02, 100.2, array([1, 0, 0])), 
            (-4.02, 300.4, array([1, 1, 0])), 
            (3.57, 503.7, array([1, 0, 1])), 
            (0.01, 700.0, array([0, 0, 0]))]

for f in range(len(test)):
sorted(test[f], key=lambda item: abs(item[0]), reverse=True)

TypeError: 'float' object is unsubscriptable

Any ideas?
awa

Recommended Answers

Hi, I want to sort a tuple consisting of two floats and one array by the absolute value of the first float in reverse order. What I did is the following:

from numpy import *

test = [(-0.02, 100.2, array([1, 0, 0])), 
            (-4.02, 300.4, array([1, 1, 0])), 
            (3.57, …
Jump to Post

All 3 Replies

Hi, I want to sort a tuple consisting of two floats and one array by the absolute value of the first float in reverse order. What I did is the following:

from numpy import *

test = [(-0.02, 100.2, array([1, 0, 0])), 
            (-4.02, 300.4, array([1, 1, 0])), 
            (3.57, 503.7, array([1, 0, 1])), 
            (0.01, 700.0, array([0, 0, 0]))]

for f in range(len(test)):
sorted(test[f], key=lambda item: abs(item[0]), reverse=True)

TypeError: 'float' object is unsubscriptable

Any ideas?
awa

Try

from numpy import *

test = [(-0.02, 100.2, array([1, 0, 0])), 
            (-4.02, 300.4, array([1, 1, 0])), 
            (3.57, 503.7, array([1, 0, 1])), 
            (0.01, 700.0, array([0, 0, 0]))]

print sorted(test, key=lambda item: abs(item[0]), reverse=True)

Thanks, so close but I wouldn't have thought about it for quite a time...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.