[英]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)))
您需要應用list
上filter
,像這樣print(key, len(list(filter(bool, value))))
在Python 3。
使用filter
與None
,它傳遞給它的迭代過濾掉所有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.