Skip to main content
 首页 » 编程设计

python之在 Python 中获取 key 的最大长度

2025年05月04日17tuyile006

<分区>

Possible Duplicate:
Find longest (string) key in dictionary

没有折叠。示例:

from functools import reduce 
dict = {'t1': 'test1', 'test2': 't2'} 
print(len(reduce(lambda x, y: x if len(x) >= len(y) else y, dict.keys()))) 

有没有办法在字典中获取最长键的长度(最好是在一行中)?折叠没有错,但我只是想知道在 Python 3 中是否有另一种方法可以做到这一点。