简体   繁体   English

计算一个密钥和一个python(3.2)字典的多少值

[英]Counting how many values were attributed to a key an a python (3.2) dictionary

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. 非常感谢您的帮助。

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 : 你也可以使用bool filter

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. 您需要应用listfilter ,像这样print(key, len(list(filter(bool, value))))在Python 3。

Use filter with None , it filters out all falsy values from the iterable passed to it. 使用filterNone ,它传递给它的迭代过滤掉所有falsy值。

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

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
>>>
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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM