计算有多少值归因于一个 python (3.2) 字典的键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19843457/
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
Counting how many values were attributed to a key an a python (3.2) dictionary
提问by user2962024
I am sure this is silly, but I simply cannot get around it. I have a dictionary, like this, with unequal number of values for each key:
我确信这很愚蠢,但我根本无法绕过它。我有一个像这样的字典,每个键的值数量不等:
'John greased ': ['axle', 'wheel', 'wheels', 'wheel', 'engine', ''],
'Paul alleged ': ['truth', 'crime', 'facts', 'infidelity', 'incident', ''],
'Tracy freed ': ['animals', 'fish', 'slaves', 'slaves', 'slaves', 'pizza'],
'Lisa plowed ': ['field', 'field', '', '', '', ''],
I want to know how many values there are for each key, not each unique value but how many tokens there are per key, repeated or not. So I would have a result like:
我想知道每个键有多少个值,不是每个唯一值,而是每个键有多少令牌,重复与否。所以我会得到如下结果:
John greased 5
Paul alleged 5
Tracy freed 6
Lisa plowed 2
I was trying to use this to work it out using the code bellow:
我试图用下面的代码来解决这个问题:
for key, value in sorted(result.items()):
print(key, len(value))
But because of the missing values all the lengths turn out to be the same. Any ideas on how to solve this or where to find it out? Thanks a lot for any help.
但由于缺失值,所有长度结果都相同。关于如何解决这个问题或在哪里找到它的任何想法?非常感谢您的帮助。
采纳答案by Games Brainiac
One way to solve this, is by changing your last line:
解决此问题的一种方法是更改最后一行:
print(key, len([item for item in value if item]))
So your complete code:
所以你的完整代码:
ITEMS = {
'John greased ': ['axle', 'wheel', 'wheels', 'wheel', 'engine', ''],
'Paul alleged ': ['truth', 'crime', 'facts', 'infidelity', 'incident', ''],
'Tracy freed ': ['animals', 'fish', 'slaves', 'slaves', 'slaves', 'pizza'],
'Lisa plowed ': ['field', 'field', '', '', '', ''],
}
for key, value in ITEMS.items():
#print value
print(key, len([item for item in value if item]))
You can also use filter
with bool
:
您还可以使用filter
带bool
:
print(key, len(filter(bool, value)))
So, the loop:
所以,循环:
for key, value in ITEMS.items():
#print value
print(key, len(filter(bool, value)))
You need to apply list
over filter
like so print(key, len(list(filter(bool, value))))
in Python 3.
您需要申请list
过filter
像这样print(key, len(list(filter(bool, value))))
在Python 3。
回答by Ashwini Chaudhary
Use filter
with None
, it filters out all falsy values from the iterable passed to it.
使用filter
with None
,它从传递给它的迭代中过滤掉所有虚假值。
In Python3 filter
returns an iterator so you should call list()
on it.:
在 Python3 中filter
返回一个迭代器,所以你应该调用list()
它。:
>>> lis = ['field', 'field', '', '', '', '']
>>> list(filter(None, lis))
['field', 'field']
>>> len(list(filter(None, lis)))
2
Code:
代码:
>>> my_dict = {
'John greased ': ['axle', 'wheel', 'wheels', 'wheel', 'engine', ''],
'Paul alleged ': ['truth', 'crime', 'facts', 'infidelity', 'incident', ''],
'Tracy freed ': ['animals', 'fish', 'slaves', 'slaves', 'slaves', 'pizza'],
'Lisa plowed ': ['field', 'field', '', '', '', ''],
}
for k,v in my_dict.items():
print (k, len(list(filter(None, v))))
...
Paul alleged 5
Lisa plowed 2
John greased 5
Tracy freed 6
Timing comparision between filter(None,..)
and list comprehension:
filter(None,..)
和列表理解之间的时间比较:
>>> lis = ['field', 'field', '', '', '', '']*100
>>> %timeit list(filter(None, lis))
10000 loops, best of 3: 22.2 us per loop
>>> %timeit [item for item in lis if item]
10000 loops, best of 3: 53.1 us per loop
>>> lis = ['field', 'field', '', '', '', '']*10000
>>> %timeit list(filter(None, lis))
100 loops, best of 3: 2.36 ms per loop
>>> %timeit [item for item in lis if item]
100 loops, best of 3: 5.22 ms per loop
回答by MONTYHS
data = {
'John greased ': ['axle', 'wheel', 'wheels', 'wheel', 'engine', ''],
'Paul alleged ': ['truth', 'crime', 'facts', 'infidelity', 'incident', ''],
'Tracy freed ': ['animals', 'fish', 'slaves', 'slaves', 'slaves', 'pizza'],
'Lisa plowed ': ['field', 'field', '', '', '', '']
}
for each in data:
i = 0
print each
for item in data[each]:
if len(item) > 0:
i =i +1
print i
回答by MONTYHS
Look at this:
看这个:
>>> dct = {'John greased ': ['axle', 'wheel', 'wheels', 'wheel', 'engine', ''],
... 'Paul alleged ': ['truth', 'crime', 'facts', 'infidelity', 'incident', ''],
... 'Tracy freed ': ['animals', 'fish', 'slaves', 'slaves', 'slaves', 'pizza'],
... 'Lisa plowed ': ['field', 'field', '', '', '', '']}
>>>
>>> {k:sum(1 for x in v if x) for k,v in dct.items()}
{'Paul alleged ': 5, 'Lisa plowed ': 2, 'John greased ': 5, 'Tracy freed ': 6}
>>>
>>> for key,value in dct.items():
... print(key, sum(1 for v in value if v))
...
Paul alleged 5
Lisa plowed 2
John greased 5
Tracy freed 6
>>>