Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中获取键的顶部长度_Python_Python 3.x_Dictionary_String Length - Fatal编程技术网

在Python中获取键的顶部长度

在Python中获取键的顶部长度,python,python-3.x,dictionary,string-length,Python,Python 3.x,Dictionary,String Length,可能重复: 没有折叠。例如: 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中是否有其他方法可以实现折叠。您只需简单地完成即可 max(map(len,

可能重复:

没有折叠。例如:

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中是否有其他方法可以实现折叠。

您只需简单地完成即可

max(map(len, my_dict))
这将获取所有关键帧的长度,并提取这些长度的最大值

要获取最长的密钥本身,请使用

max(my_dict, key=len)
你可以简单地做

max(map(len, my_dict))
这将获取所有关键帧的长度,并提取这些长度的最大值

要获取最长的密钥本身,请使用

max(my_dict, key=len)

有没有办法只获取长度为3个字母的钥匙?有没有办法只获取长度为3个字母的钥匙?