Update: I have already accepted the answer. However, if anyone could explain about why I got error message "keyerror:1 " in my method 2 in the question, I will be very happy with that. Thank you!
----------------original question ----------------
I 'm completing one exercise, but whatever I have tried, nothing was correct. I went through all those available answers, still I could not figure it out. The expected answer is described hereinbelow:
The exercise template contains a class named Car which represents the features of a car through two attributes: make (str) and top_speed (int).
Please write a function named fastest_car(cars: list) which takes a list of Car objects as its argument.
The function should return the make of the fastest car. You may assume there will always be a single car with the highest top speed. Do not change the list given as an argument, or make any changes to the Car class definition.
You may use the following code to test your function:
if __name__ == "__main__":car1 = Car("Saab", 195)car2 = Car("Lada", 110)car3 = Car("Ferrari", 280)car4 = Car("Trabant", 85)cars = [car1, car2, car3, car4]print(fastest_car(cars))# Ferrari
I have tried like this:
class Car:def __init__(self,make:str,top_speed:int):self.make=makeself.top_speed=top_speeddef fastest_car(self,list):list.sort(key = lambda x : x.top_speed)print("Sort by speed")print(list)if __name__ == "__main__":car1 = Car("Saab", 195)car2 = Car("Lada", 110)car3 = Car("Ferrari", 280)car4 = Car("Trabant", 85)cars = [car1, car2, car3, car4]Car.fastest_car(cars[-1])#error message: TypeError: fastest_car() missing 1 required positional argument: 'list'
I also tried normal method:
#method 2cars=[{'Saab':195},{'Lada':110},{'Ferrari':280},{'Trabant':85}]a=sorted(cars,key = lambda x : x[1])print(a)#just print the sorted listerror message:----> 3 a=sorted(cars,key = lambda x : x[1])4 print(a)KeyError: 1
If you could kindly pointed out what I did wrong, I will highly appreciated. Thank You!
point out mistakes and gain the knowledge
Best Answer
just use max() function:
fastest_car = max(cars, key=lambda x: x.top_speed)print(fastest_car.make)print(fastest_car.top_speed)>>> out'''Ferrari280
UPD
for list of dictionaries you can try this:
cars=[{'Saab':195},{'Lada':110},{'Ferrari':280},{'Trabant':85}]d = {}for i in cars:d.update(i)max(d,key=d.get) # 'Ferrari'
So thanks to @12944qwerty and @Barmer's help, this problem is solved like this:
class Car:def __init__(self,make:str,top_speed:int):self.make=makeself.top_speed=top_speed@staticmethoddef fastest_car(list):list.sort(key = lambda x : x.top_speed)print("Sort by speed")return list[-1]def __repr__(self):return self.makeif __name__ == "__main__":car1 = Car("Saab", 195)car2 = Car("Lada", 110)car3 = Car("Ferrari", 280)car4 = Car("Trabant", 85)cars = [car1, car2, car3, car4]print(Car.fastest_car(cars))#result Sort by speedFerrari
I tried also to take that @staticmethod away and I still get the same result. I hope that somebody could explain a little bit why and if possible, someone could tell me what the error message means in that normal method I tried, KeyError: 1Thanks!