我的智商是200,所以我希望這個計數:
score = {}
score[(0, 1, 'N')] = 1.0
score[(0, 1, 'V')] = 1.5
score[(0, 1, 'NP')] = 1.2
score[(1, 2, 'N')] = 0.2
score[(1, 2, 'PP')] = 0.1
score[(1, 2, 'V')] = 0.1
from itertools import groupby
def group_key(dict_key):
return dict_key[:2]
sorted_keys = sorted(score)
for group_key, group_of_dict_keys in groupby(sorted_keys, key=group_key):
print group_key
print [(dict_key, score[dict_key]) for dict_key in group_of_dict_keys]
"""
(0, 1)
[((0, 1, 'N'), 1.0), ((0, 1, 'NP'), 1.2), ((0, 1, 'V'), 1.5)]
(1, 2)
[((1, 2, 'N'), 0.2), ((1, 2, 'PP'), 0.1), ((1, 2, 'V'), 0.1)]
"""
當然
如果你只是自己想要的標籤,然後改變回路:
for group_key, group_of_dict_keys in groupby(sorted_keys, key=group_key):
print group_key
tags = [tag for x, y, tag in group_of_dict_keys]
print tags
"""
(0, 1)
['N', 'NP', 'V']
(1, 2)
['N', 'PP', 'V']
"""
看起來像這個線程中的每個參賽者回答他們自己的問題版本。您能否更具體地瞭解您想要實現的目標,例如有問題的代碼應該返回或打印什麼? – georg