Input: Zantro@16.15 Zity@12.5 Gamry@9.8
Output conditions:
The name and mileage of certain cars are passed as input. The format is CARNAME@MILEAGE and the input is as a single line, with each car information separated by a space. The program must print the car with the lowest mileage.
You can use split
function to seperate carnames
and mileage
and compare.
s = input().split(" ")
d = {}
l = []
for data in s:
value, key = data.split("@")
d[key] = value
l.append(float(key))
print(d[str(min(l))])
Roughly speaking, here is the spoon I am feeding you with:
So you take the input split on every space character.
entries = the_input.split() # str.split with no arg splits on whitespace
Now you have a list of "Car@Number". Loop over that, split by "@" symbol.
car_name, mileage = entry.split("@")
mileage = float(mileage) # parse the string into a floating point number
Note: you want to loop entries and put each into List you create earlier, for instance: complete_entries
, like so
complete_entries.append((car_name, mileage))
to fill the list with tuples
of (car_name, mileage)
then you can do
print(sorted(complete_entries, key=lambda x: x[1])
To print a new sorted
list where key
is the lambda
function that takes one parameter x
and indexes the seconds element of x ( x[1]
) which corresponds to the mileage
field of the tuple.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.