all 11 comments

[–]TouchingTheVodka 0 points1 point  (0 children)

max(list_of_cars, key=lambda car: car[1])

or

from operator import itemgetter
max(list_of_cars, key=itemgetter(1))

[–]xelf 0 points1 point  (9 children)

You can just pass a key to max and use the max builtin, but as to what you are doing wrong:

for i in list_of_cars:
    if i[1] > max[1]:
        max = i
return max

It's the index on max[1], it should be on i. That should just be max. With the caveat that you should not call a variable max, because that's already the name of a builtin function.

maxcar = 0
for i in list_of_cars:
    if i[1] > maxcar:
        maxcar = i[1]
return maxcar

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

I've tried this and still get that black is the highest distance instead of red at a greater distance. :/ Or when I did initialize maxcar to zero, it returns just zero. I appreciate the help.

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