我想了解Python字典上max函数的操作.以下是我正在使用的代码:
tall_buildings = {
"Empire State": 381, "Sears Tower": 442,
"Burj Khalifa": 828, "Taipei 101": 509
}
# 1. find the height of the tallest building
print("Height of the tallest building: ", max(tall_buildings.values()))
# 2. find the name, height pair that is tallest
print(max(tall_buildings.items(), key=lambda b: b[1]))
# 3. find the tallest building
print(max(tall_buildings, key=tall_buildings.get))
上面所有的打印语句都给出了正确的结果,如代码中的注释所示.
我了解#1和#2的工作原理.
1: tall_buildings.values() gives a stream of heights and max function returns the max of the heights.
2: tall_buildings.items() gives a stream of (name, height) pairs and max function returns the pair based on the key=pair's height.
但是,我很难理解#3的工作原理. key = tall_buildings.get如何用作找到最高建筑物的钥匙?