繁体   English   中英

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

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

我确信这很愚蠢,但我根本无法绕过它。 我有一个像这样的字典,每个键的值不等:

'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', '', '', '', ''],

我想知道每个键有多少值,而不是每个唯一值,但每个键有多少个标记,重复或不重复。 所以我会得到一个结果:

John greased  5
Paul alleged  5
Tracy freed  6
Lisa plowed  2

我试图使用它来使用下面的代码来解决它:

for key, value in sorted(result.items()):
         print(key, len(value)) 

但由于缺失值,所有长度都变得相同。 关于如何解决这个或在哪里找到它的任何想法? 非常感谢您的帮助。

解决这个问题的一种方法是改变你的最后一行:

print(key, len([item for item in value if item])) 

那么你的完整代码:

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

你也可以使用bool filter

print(key, len(filter(bool, value)))

所以,循环:

for key, value in ITEMS.items():
    #print value
    print(key, len(filter(bool, value)))

您需要应用listfilter ,像这样print(key, len(list(filter(bool, value))))在Python 3。

使用filterNone ,它传递给它的迭代过滤掉所有falsy值。

在Python3 filter返回一个迭代器,所以你应该调用它上面的list()

>>> lis = ['field', 'field', '', '', '', '']
>>> list(filter(None, lis))
['field', 'field']
>>> len(list(filter(None, lis)))
2

码:

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

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

看这个:

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