计算一个python(3.2)字典中键的值

2024-09-27 07:17:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我肯定这很傻,但我就是绕不过去。我有一个字典,像这样,每个键的值的数目不相等:

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

但是由于缺少值,所有的长度都是一样的。 有什么办法解决这个问题或者在哪里找到答案吗?非常感谢你的帮助。


Tags: keyfield字典valuejohnwheellisa数目
3条回答

^{}None一起使用,它将从传递给它的iterable中筛选出所有错误值。

在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

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

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

也可以将filterbool一起使用:

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

所以,循环:

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

您需要像Python 3中的print(key, len(list(filter(bool, value))))那样,在filter上应用list

看看这个:

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

相关问题 更多 >

    热门问题