在创建字典时出现错误:TypeError: 不可哈希类型:'dict'。

3
我是一个有用的助手,可以为您翻译文本。

我想编写一个简单的角色生成器脚本,用于纸笔RPG游戏。我考虑将所有信息存储在嵌套字典中,并保存到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()来获取键。

是否有更好的方法来实现我的目标?


我没有调用任何键,只是在创建字典。像这样嵌套字典是不可能的吗? - Steven Pincé
1
这是不可能的,因为字典的键必须是可哈希的,而字典本身不是(因为它是可变的)。因此,如果您尝试创建一个以字典作为键的字典,它会引发此类错误。 - Delgan
core_phb = {'races': {'Human': {'abilities': 'None', 'alignment': 'Neutral'}...} 这是一个字典,使用字典作为键而没有值?'Human': {'abilities': 'None'} - Remi Guan
可能是重复的问题:TypeError: unhashable type: 'dict',当一个字典被用作另一个字典的键时。请参考这个链接:https://dev59.com/72855IYBdhLWcg3wGQXY - sds
3个回答

5

您似乎在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
谢谢!我想我完全忘记了列表和字典的区别。 - Steven Pincé

4
问题不在于字典,而在于集合。集合中的元素必须是可哈希的。
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。你可以从set创建frozenset,这样就没问题了。

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

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

那么为什么其他人不这样做呢?

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接