2016-11-14 46 views
0

我有一個詞典列表,我想查找列表中的項目頻率。我只是用了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' 
+0

你能告訴'final_list'的初始化和舉個例子投入產出? – WorldSEnder

+0

'print(final_list)'並添加結果的問題。你不能使用嵌套字典。 – furas

+1

使用'namedtuple'而不是'dep_dict'的字典 –

回答

1

爲了詳細說明在評論中提到的namedtuple

from collections import namedtuple 

mytuple = namedtuple('mytuple', (
    'parent_pos', 'parent_dep', 'child', 'child_pos', 
    'child_dep', 'avl_sent', 'avl_author_type', 'avl_brand_1' 
)) 
for dep in dep_list: 
    dep_tokens = dep.split(',') 
    dep_tuple = mytuple(
     parent=dep_tokens[0], 
     parent_pos=dep_tokens[1], 
     parent_dep=dep_tokens[2], 
     child=dep_tokens[3], 
     child_pos=dep_tokens[4], 
     child_dep=dep_tokens[5], 
     avl_sent=item['avl_sent'], 
     avl_author_type=item['avl_author_type'] 
     avl_brand_1=item['avl_brand_1'] 
    ) 
    final_list.append(dep_tuple) 
counts = collections.Counter(final_list) 
print counts 
0

你可以得到這樣的項目的頻率:

from collections import Counter 
import json 

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'}] 

counter = Counter() 
for d in final_list: 
    counter.update(d.values()) 

print(json.dumps(counter, indent=4)) 

輸出:

{ 
    "VBD": 8, 
    "showdown": 2, 
    "it": 1, 
    "individual": 11, 
    "JJ": 1, 
    "DT": 1, 
    "amod": 1, 
    "event": 2, 
    "networking": 1, 
    "nn": 2, 
    "positive": 11, 
    "nsubj": 2, 
    "emergingleaders": 1, 
    "Virtua": 6, 
    "PRP": 3, 
    "Kennedy Health": 5, 
    "final": 1, 
    "do": 5, 
    "we": 1, 
    "get": 3, 
    "company": 2, 
    "brewing": 1, 
    "NN": 8, 
    "this": 1, 
    "NNS": 1, 
    "us": 1, 
    "dobj": 4, 
    "pobj": 5, 
    "root": 8 
} 
+0

也可以使用'from pprint import pprint'和'pprint(counter)' –

+0

@JohnLaRooy:我知道,但我更喜歡'json.dumps'輸出更好 - 不管,它不是解決方案的重要組成部分。 – martineau