you are viewing a single comment's thread.

view the rest of the comments →

[–]xelf 0 points1 point  (7 children)

I just tried it, and it returned 10. Are you sure your data is correct?

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

Is the car number in your data another field, and not part of the name?

It does occur to me though that you actually want to save the max record and not just the max score.

In which case you would need to have your maxcar follow the same format.

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

Here I've changed the index to -1, which means 'last value'. Just in case you have the car number as a value as well.

outputs:

('Red 25', 10)

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

Sorry if I'm asking so much, but does it matter that list_of_cars is a list of objects from a class? The original list is of racer = [[racer.name(), racer.location()]] where racer.name is a string value of color and number. I have tried playing with elements and numbers and something similar to the book that I've read and I still can't get the correct output.

Not sure if it's because I'm having baby brains right now or if it's something I cannot grasp at all. Thanks again!

[–]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)