I am working on a piece of Python code that consists of allowing the user to enter the top speed for 3 different cars, then prints out the car's name and top speed sorted in ascending order.
So far, two lists have been created
carName= ["Ferrari", "Lambo", "Porsche"]
and
carSpeed= ["245.5794", "242.4616", "318.1555"]
The carSpeed has been rounded to 2 decimal place with
carSpeedTwoDP= ['%.2f' % elem for elem in carSpeed]
And that list, along with carName, has been made into a tuple
carSpeedSorted= sorted(zip(carSpeed, carName))
I then print out the tuple with
for i in range (3):
print "%s"% (carSpeedSorted[i],)
This poses a problem however. The table is supposed to show
Lambo 242.46
Ferrari 245.58
Porsche 318.16
But because the speed is first in the tuple (as the list must be sorted in ascending speed order and tuples sort themselves with the first elements they can find), the display is the following:
('242.46', 'Lambo')
('245.58', 'Ferrari')
('318.16', 'Porsche ')
I have been researching for a while, but haven't been able to come up with a solution to invert the lists so that the car name shows up first, all the while keeping the tuple sorted by speed.
Any help would be greatly appreciated! Thank you!