创建词典时:TypeError:unhashable type:' dict'

时间:2015-11-07 08:59:56

标签: python python-2.7 dictionary

我试图编写一个简单的Character Generator脚本,我可以用它来制作Pen and Paper RPG&#39}。我正在考虑将所有信息存储在嵌套字典中并将其保存到JSON文件中。

但是,在创建以下字典时,我收到错误:

nhashable type: 'dict', focussing on {'cha': 1}}}
core_phb = {
'races': {
    'Human': {
        {'abilities': 'None'},
        {'alignment': 'Neutral'},
        {'size': 'Medium'},
        {'speed': 6},
        {'languages': 'Common'},
        {'ability_modifiers': {
            {'str': 1},
            {'dex': 1},
            {'con': 1},
            {'int': 1},
            {'wis': 1},
            {'cha': 1}}}
    },
    'Dwarf': {
        {'abilities': [
            'ability1',
            'ability2'
            ]},
        {'alignment': 'Lawful Good'},
        {'size': 'Medium'},
        {'speed': 5},
        {'languages': [
            'Common',
            'Dwarven'
            ]},
        {'ability_modifiers': [
            {'con': 2},
            {'wis': 1}
            ]}
    },
    'Elf': {
        {'abilities': [
            'ability1',
            'ability2'
            ]},
        {'alignment': 'Chaotic Good'},
        {'size': 'Medium'},
        {'speed': 6},
        {'languages': [
            'Common',
            'Elven'
            ]},
        {'ability_modifiers': [
            {'dex': 2},
            {'int': 1}
            ]}
    }
},
'classes': {
    {'Fighter': {}},
    {'Ranger': {}},
    {'Wizard': {}}
},
'ability_scores': [
    {'Str': 'str'},
    {'Dex': 'dex'},
    {'Con': 'con'},
    {'Int': 'int'},
    {'Wis': 'wis'},
    {'Cha': 'cha'}]
}

我只是想创建字典,而不是从中调用任何键。

据我所知TypeError: unhashable type: 'dict',我可以使用frozenset()获取密钥。

有没有更好的方法来做我想做的事情?

3 个答案:

答案 0 :(得分:4)

您似乎正在为Python错误地制作词典{...}

列表如下所示:

[ {'a': 1}, {'b': 1}, {'c': 1} ]

字典看起来像这样:

{ 'a': 1, 'b': 2, 'c': 3 }

如果我猜测你想要的行为,那么你可能想要这样的东西:

human = {
    'abilities': 'None',
    'alignment': 'Neutral',
    'size': 'Medium',
    'speed': 6,
    'languages': 'Common',
    'ability_modifiers': {
        'str': 1,
        'dex': 1,
        'con': 1,
        'int': 1,
        'wis': 1,
        'cha': 1
    }
}

答案 1 :(得分:3)

问题不在于dict,而在于setset的元素必须是可清除的。在

core_phb = {
'races': {
    'Human': {
        {'abilities': 'None'},
        {'alignment': 'Neutral'},
        {'size': 'Medium'},
        {'speed': 6},
        {'languages': 'Common'},
        {'ability_modifiers': {
            {'str': 1},
            {'dex': 1},
            {'con': 1},
            {'int': 1},
            {'wis': 1},
            {'cha': 1}}}
    },
    'Dwarf': {
        {'abilities': [
            'ability1',
            'ability2'
            ]},
        {'alignment': 'Lawful Good'},
        {'size': 'Medium'},
        {'speed': 5},
        {'languages': [
            'Common',
            'Dwarven'
            ]},
        {'ability_modifiers': [
            {'con': 2},
            {'wis': 1}
            ]}
    },
    'Elf': {
        {'abilities': [
            'ability1',
            'ability2'
            ]},
        {'alignment': 'Chaotic Good'},
        {'size': 'Medium'},
        {'speed': 6},
        {'languages': [
            'Common',
            'Elven'
            ]},
        {'ability_modifiers': [
            {'dex': 2},
            {'int': 1}
            ]}
    }
},
'classes': {
    {'Fighter': {}},
    {'Ranger': {}},
    {'Wizard': {}}
},
'ability_scores': [
    {'Str': 'str'},
    {'Dex': 'dex'},
    {'Con': 'con'},
    {'Int': 'int'},
    {'Wis': 'wis'},
    {'Cha': 'cha'}]
}

密钥正常,但该值为非法set,因为其元素为dict s。您可以从frozenset生成set,然后就可以了。

{frozenset({1})}
{frozenset({1})}
{{1}}
Traceback (most recent call last):
  Python Shell, prompt 7, line 1
builtins.TypeError: unhashable type: 'set'

答案 2 :(得分:2)

我想这个:

'Human': {
    {'abilities': 'None'},
    {'alignment': 'Neutral'},
    {'size': 'Medium'},
    {'speed': 6},
    {'languages': 'Common'},
    {'ability_modifiers': {
        {'str': 1},
        {'dex': 1},
        {'con': 1},
        {'int': 1},
        {'wis': 1},
        {'cha': 1}}}
},

应该是一个清单。否则,每个逗号分隔的元素都是您尝试存储在集合中的可变元素。你已经在最后一个条目中做到了这一点:

'ability_scores': [
{'Str': 'str'},
{'Dex': 'dex'},
{'Con': 'con'},
{'Int': 'int'},
{'Wis': 'wis'},
{'Cha': 'cha'}]

那为什么不是所有其他人呢?