问题遇到的现象和发生背景 python动物重量排序
用代码块功能插入代码,请勿粘贴截图
我想要达到的结果


ls = []
while True:
temp = input()
if temp == '':
break
ls.append(temp.split())
print(sorted(ls,key=lambda weight:float(weight[1][:-1]) * 1000 if weight[1][-1] == 't' else int(weight[1][:-2])))
animals = []
while True:
str1 = input()
# print(str1)
if str1 == '':
break
else:
animal, weight = str1.split()
if 't' in weight:
w = weight[:-1]
unit = weight[-1]
# 将单位转为kg,方便进行排序
weight_kg = float(w) * 1000
animals.append([animal, w, unit, weight, weight_kg])
else:
w = weight[:-2]
unit = weight[-2:]
weight_kg = float(w)
animals.append([animal, w, unit, weight, weight_kg])
# print(animals)
# print(sorted(animals, key=lambda x: x[4]))
# 按重量(单位kg)排序
animals = sorted(animals, key=lambda x: x[4])
for i in range(len(animals)):
animals[i] = [animals[i][0], animals[i][3]]
print(animals)