Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
给定一个字典d,其中键值对由作为键的字符串和作为值的整数组成,我想打印值为最大值的键字符串。
d
当然,我可以循环d.items(),存储最大值及其键并在for循环后输出后者。但是是否有一种更“pythonic”的方式只使用一个max函数结构,比如
d.items()
for
max
print max(...)
print max(d.keys(), key=lambda x: d[x])
甚至更短(来自评论):
print max(d, key=d.get)