pol*_*eer 1 python dictionary string-length python-3.x
可能重复:
在字典中查找最长(字符串)键
没有折叠.例:
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())))
Run Code Online (Sandbox Code Playgroud)
有没有办法在字典中获得最长的密钥长度(最好是在一行中)?折叠没有任何问题,但我只是感兴趣,如果在Python 3中有另一种方法可以做到这一点.
你可以干脆做
max(map(len, my_dict))
Run Code Online (Sandbox Code Playgroud)
这将占用所有键的长度并提取这些长度的最大值.
要获得最长的密钥,请使用
max(my_dict, key=len)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1710 次 |
| 最近记录: |