我想获取具有最高价值的数据。
class Mountain:
def __init__(self, name, height):
self.name = name
self.height = height
mountain_01 = Mountain("Mount Everest", 8850)
mountain_02 = Mountain("Mount Kazbek", 5033)
mountain_03 = Mountain("Vinson Massif", 4892)
mountain_04 = Mountain("Kilimanjaro", 5895)
highest_mountain = max(mountain_01.height, mountain_02.height,
mountain_03.height, mountain_04.height)
print(f"Highest is : {highest_mountain}")
我希望输出为“最高是:珠穆朗玛峰8850”
答案 0 :(得分:0)
您可以先将所有山脉捕获在一个列表中,然后使用该键作为山脉的高度,然后在该列表中计算max
#List of mountains
mountains = [mountain_01, mountain_02, mountain_03, mountain_04]
#Get highest mountain based on height of mountain
highest_mountain = max(mountains, key=lambda mountain:mountain.height)
print(highest_mountain.name)
print(highest_mountain.height)
输出将为
Mount Everest
8850