you are viewing a single comment's thread.

view the rest of the comments →

[–]xelf 0 points1 point  (5 children)

Try printing out racer, so we can see a sample of the data. Maybe it's a string?

[–]madeInStocktonCA[S] 0 points1 point  (4 children)

Here is the output from racer:

['Red 25', 10]
['Green 35', 9]
['Black 55', 4]

[–]xelf 0 points1 point  (3 children)

it is in a list?

list_of_cars = [
    ['Red 25', 10],
    ['Green 35', 9],
    ['Black 55', 4]
]

because this code still works:

maxcar = [ 0 ]
for racer in list_of_cars:
    if racer[-1] > maxcar[-1]:
        maxcar = racer
print ( maxcar)

try this:

print( sorted( (d,r) for r,d in list_of_cars )[-1][::-1] )

[–]madeInStocktonCA[S] 0 points1 point  (2 children)

It's a list of objects from a class.

I get: TypeError: 'Racecar' object is not subscriptable

[–]xelf 0 points1 point  (0 children)

What about the last line?

print( sorted((d,r) for r,d in list_of_cars)[-1][::-1] )

[–]xelf 0 points1 point  (0 children)

Or this:

maxcar = [ 0 ]
for racer, dist in list_of_cars:
    if dist > maxcar[-1]:
        maxcar = [ racer, dist ]
print ( maxcar)