我有一個詞典列表,我想查找列表中的項目頻率。我只是用了collections.Counter()
,但它拋出一個錯誤說:在詞典表中查找項目頻率
unhashable type: 'dict'
我的代碼:
for dep in dep_list:
dep_dict['parent'] = dep.split(',')[0]
dep_dict['parent_pos'] = dep.split(',')[1]
dep_dict['parent_dep'] = dep.split(',')[2]
dep_dict['child'] = dep.split(',')[3]
dep_dict['child_pos'] = dep.split(',')[4]
dep_dict['child_dep'] = dep.split(',')[5]
dep_dict['avl_sent'] = item['avl_sent']
dep_dict['avl_author_type'] = item['avl_author_type']
dep_dict['avl_brand_1'] = item['avl_brand_1']
final_list.append(dep_dict.copy())
counts = collections.Counter(final_list)
print counts
final_list
內容正是我想要的。我只想要頻率。我想最終輸出一個json作爲一個json。
有人可以幫助我嗎?
編輯
樣本輸出後print final_list
[{'parent_pos': u'VBD', 'avl_author_type': u'individual', 'parent': u'get', 'avl_sent': u'positive', 'parent_dep': u'root', 'child_pos': u'NN', 'child_dep': u'dobj', 'child': u'event', 'avl_brand_1': u'Kennedy Health'}, {'parent_pos': u'VBD', 'avl_author_type': u'individual', 'parent': u'get', 'avl_sent': u'positive', 'parent_dep': u'root', 'child_pos': u'NNS', 'child_dep': u'dobj', 'child': u'emergingleaders', 'avl_brand_1': u'Kennedy Health'}, {'parent_pos': u'VBD', 'avl_author_type': u'individual', 'parent': u'get', 'avl_sent': u'positive', 'parent_dep': u'root', 'child_pos': u'NN', 'child_dep': u'pobj', 'child': u'company', 'avl_brand_1': u'Kennedy Health'}, {'parent_pos': u'NN', 'avl_author_type': u'individual', 'parent': u'event', 'avl_sent': u'positive', 'parent_dep': u'dobj', 'child_pos': u'NN', 'child_dep': u'nn', 'child': u'networking', 'avl_brand_1': u'Kennedy Health'}, {'parent_pos': u'NN', 'avl_author_type': u'individual', 'parent': u'company', 'avl_sent': u'positive', 'parent_dep': u'pobj', 'child_pos': u'NN', 'child_dep': u'nn', 'child': u'brewing', 'avl_brand_1': u'Kennedy Health'}, {'parent_pos': u'VBD', 'avl_author_type': u'individual', 'parent': u'do', 'avl_sent': u'positive', 'parent_dep': u'root', 'child_pos': u'DT', 'child_dep': u'dobj', 'child': u'this', 'avl_brand_1': u'Virtua'}, {'parent_pos': u'VBD', 'avl_author_type': u'individual', 'parent': u'do', 'avl_sent': u'positive', 'parent_dep': u'root', 'child_pos': u'NN', 'child_dep': u'pobj', 'child': u'showdown', 'avl_brand_1': u'Virtua'}, {'parent_pos': u'VBD', 'avl_author_type': u'individual', 'parent': u'do', 'avl_sent': u'positive', 'parent_dep': u'root', 'child_pos': u'PRP', 'child_dep': u'pobj', 'child': u'us', 'avl_brand_1': u'Virtua'}, {'parent_pos': u'VBD', 'avl_author_type': u'individual', 'parent': u'do', 'avl_sent': u'positive', 'parent_dep': u'root', 'child_pos': u'PRP', 'child_dep': u'nsubj', 'child': u'we', 'avl_brand_1': u'Virtua'}, {'parent_pos': u'VBD', 'avl_author_type': u'individual', 'parent': u'do', 'avl_sent': u'positive', 'parent_dep': u'root', 'child_pos': u'PRP', 'child_dep': u'nsubj', 'child': u'it', 'avl_brand_1': u'Virtua'}, {'parent_pos': u'NN', 'avl_author_type': u'individual', 'parent': u'showdown', 'avl_sent': u'positive', 'parent_dep': u'pobj', 'child_pos': u'JJ', 'child_dep': u'amod', 'child': u'final', 'avl_brand_1': u'Virtua'}]
ERROR in prepare final output
unhashable type: 'dict'
你能告訴'final_list'的初始化和舉個例子投入產出? – WorldSEnder
'print(final_list)'並添加結果的問題。你不能使用嵌套字典。 – furas
使用'namedtuple'而不是'dep_dict'的字典 –