如何找到字典值的长度 [英] How to find length of dictionary values

查看:1436
本文介绍了如何找到字典值的长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对所有这些都很新鲜,所以这可能是一个noobie的问题..但是我正在寻找字典值的长度...但是我不知道这可以做到如何。

$ b所以例如,

  d = {'key':['hello','勇敢的','早晨','日落','形而上学'] 

一种我可以找到字典值的 len 的数量。



谢谢

解决方案

当然可以。在这种情况下,您只需执行以下操作:

  length_key = len(d ['key'])存储在`'key'` ... 

很难说为什么你真的想要这个,但是也许创建另一个将键映射到值的长度是有用的:

  length_dict = {key:len (value)为key,value in d.items()} 
length_key = length_dict ['key']#存储在key的列表的长度...


I am pretty new to all of this so this might be a noobie question.. but I am looking to find length of dictionary values... but I do not know how this can be done.

So for example,

d = {'key':['hello', 'brave', 'morning', 'sunset', 'metaphysics']}

I was wondering is there a way I can find the len or number of items of the dictionary value.

Thanks

解决方案

Sure. In this case, you'd just do:

length_key = len(d['key'])  # length of the list stored at `'key'` ...

It's hard to say why you actually want this, but, perhaps it would be useful to create another dict that maps the keys to the length of values:

length_dict = {key: len(value) for key, value in d.items()}
length_key = length_dict['key']  # length of the list stored at `'key'` ...

这篇关于如何找到字典值的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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