5

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

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

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

4

4 回答 4

18

解决此问题的一种方法是更改​​最后一行:

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

您还可以filter使用bool

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

所以,循环:

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

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

于 2013-11-07T18:12:13.103 回答
3

使用filterwith None,它会从传递给它的迭代中过滤掉所有虚假值。

在 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
于 2013-11-07T18:12:18.500 回答
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
>>>
于 2013-11-07T18:17:36.520 回答
2
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
于 2013-11-07T18:15:45.973 回答