我确定这很愚蠢,但是我根本无法解决。我有一个像这样的词典,每个键的值数量不相等:

'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

我试图用它使用代码bellow来解决它:

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

您也可以使用 filterbool:

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

因此,循环:

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

您需要申请 list 超过 filter 像这样 print(key, len(list(filter(bool, value)))) 在Python 3中。

其他提示

看这个:

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

利用 filterNone, ,它滤除了传递给它的所有虚假值。

在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
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归因
不隶属于 StackOverflow
scroll top