Python-查找字典中最长(最多单词)的键 [英] Python - Find longest (most words) key in dictionary

查看:917
本文介绍了Python-查找字典中最长(最多单词)的键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以快速查询字典对象,以便找到词最多的键(所有键都是字符串类型)?

Is there a way to quickly query a dictionary object in order to find the key (all keys are of string type) with the most words?

也就是说,如果键最大的项有五个词{'this is the first key':3},我该如何快速查询字典并返回整数'5'?

I.e., if the item with the largest key had five words {'this is the largest key': 3}, how could I quickly query the dict and have the int '5' returned?

最好, 乔治娜

推荐答案

最大值-每个键的单词数:

max of - count of words per key:

max(len(k.split()) for k in d.keys())

这篇关于Python-查找字典中最长(最多单词)的键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆