'language_counter':Counter()TypeError:无法散列的类型:'dict'

时间:2019-11-17 05:51:50

标签: python

我写了这个Python脚本

with open('survey_data/survey_results_public.csv', encoding='utf-8') as f:
    csv_reader = csv.DictReader(f)

    dev_type_info = {}

    for line in csv_reader:
        dev_types = line['DevType'].split(';')

        for dev_type in dev_types:
            dev_type_info.setdefault(dev_type_info, {
                'total': 0,
                'language_counter': Counter()
            })

但是我得到这个错误

> TypeError                                 Traceback (most recent call
> last) <ipython-input-34-0dd498f86be7> in <module>
>      14             dev_type_info.setdefault(dev_type_info, {
>      15                 'total': 0,
> ---> 16                 'language_counter': Counter()
>      17             })
> 
> TypeError: unhashable type: 'dict'

任何人都可以在此错误阴影下,这是什么,我该如何解决?谢谢

2 个答案:

答案 0 :(得分:2)

我想你是说:

npm run serve

因为dict.setdefault的用法是作为第一个参数的键,而不是dict本身。

for dev_type in dev_types:
  dev_type_info.setdefault(dev_type, {
    'total': 0,
    'language_counter': Counter()
   })

答案 1 :(得分:0)

您收到此错误,是因为您试图将dict用作另一个dict的键。更正下面的代码行:

dev_type_info.setdefault(dev_type_info, { ..