[英]How to obtain input like these in python? with multiple line and conditions?
Input: Zantro@16.15 Zity@12.5 Gamry@9.8输入: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.格式为 CARNAME@MILEAGE 并且输入为单行,每个汽车信息用空格分隔。 The program must print the car with the lowest mileage.程序必须打印里程数最低的汽车。
You can use split
function to seperate carnames
and mileage
and compare.您可以使用split
功能将carnames
和mileage
分开并进行比较。
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".现在您有一个“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注意:您想循环条目并将每个条目放入您之前创建的 List 中,例如: complete_entries
,像这样
complete_entries.append((car_name, mileage))
to fill the list with tuples
of (car_name, mileage)
用(car_name, mileage)
tuples
填充列表
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.打印一个新的sorted
列表,其中key
是lambda
函数,它接受一个参数x
并索引 x ( x[1]
) 的秒元素,该元素对应于元组的mileage
字段。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.