在 Python 2.7 中获取列表长度作为字典中的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17133000/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:33:51  来源:igfitidea点击:

Obtaining length of list as a value in dictionary in Python 2.7

pythonlistpython-2.7dictionary

提问by Srikanth Suresh

I have two lists and dictionary as follows:

我有两个列表和字典如下:

>>> var1=[1,2,3,4]
>>> var2=[5,6,7]
>>> dict={1:var1,2:var2}

I want to find the size of the mutable element from my dictionary i.e. the length of the value for a key. After looking up the help('dict'), I could only find the function to return number of keys i.e. dict.__len__(). I tried the Java method(hoping that it could work) i.e. len(dict.items()[0])but it evaluated to 2.

我想从我的字典中找到可变元素的大小,即键值的长度。查找后help('dict'),我只能找到返回键数即dict.__len__(). 我尝试了 Java 方法(希望它可以工作)即len(dict.items()[0])但它评估为2.

I intend to find this:
Length of value for first key: 4
Length of value for second key: 3

我打算找到这个:
第一个键4
的值长度:第二个键的值长度:3

when the lists are a part of the dictionary and not as individual lists in case their length is len(list).

当列表是字典的一部分而不是作为单独的列表时,以防它们的长度为len(list).

Any suggestions will be of great help.

任何建议都会有很大帮助。

采纳答案by Alexander Tobias Bockstaller

dict.items()is a list containing all key/value-tuples of the dictionary, e.g.:

dict.items()是一个包含字典所有键/值元组的列表,例如:

[(1, [1,2,3,4]), (2, [5,6,7])]

So if you write len(dict.items()[0]), then you ask for the length of the first tuple of that items-list. Since the tuples of dictionaries are always 2-tuples (pairs), you get the length 2. If you want the length of a value for a given key, then write:

因此,如果您编写len(dict.items()[0]),那么您会询问该项目列表的第一个元组的长度。由于字典的元组始终是 2 元组(对),因此长度为2。如果你想要给定键的值的长度,那么写:

len(dict[key])

Aso: Try not to use the names of standard types (like str, dict, setetc.) as variable names. Python does not complain, but it hides the type names and may result in unexpected behaviour.

麻生太郎:尽量不要使用标准类型的名称(如strdictset等)作为变量名。Python 不会抱怨,但它隐藏了类型名称并可能导致意外行为。

回答by Tim Pietzcker

You can do this using a dict comprehension, for example:

您可以使用dict comprehension来执行此操作,例如:

>>> var1 = [1,2,3,4]
>>> var2 = [5,6,7]
>>> d = {1:var1, 2:var2}
>>> lengths = {key:len(value) for key,value in d.iteritems()}
>>> lengths
{1: 4, 2: 3}

Your "Java" method would also nearly have worked, by the way (but is rather unpythonic). You just used the wrong index:

顺便说一句,您的“Java”方法也几乎可以工作(但相当不pythonic)。您刚刚使用了错误的索引:

>>> d.items()
[(1, [1, 2, 3, 4]), (2, [5, 6, 7])]
>>> d.items()[0]
(1, [1, 2, 3, 4])
>>> len(d.items()[0][1])
4

回答by Nullify

>>>for k,v in dict.iteritems():
   k,len(v)

ans:-

答:-

(1, 4)
(2, 3)

or

或者

>>>var1=[1,2,3,4]
>>>var2=[5,6,7]
>>>dict={1:var1,2:var2}

ans:-

答:-

>>>[len(v) for k,v in dict.iteritems()]
[4, 3]