简体   繁体   English

在创建Dictionary时:TypeError:unhashable type:'dict'

[英]While creating a Dictionary: TypeError: unhashable type: 'dict'

I'm trying to script a simple Character Generator that I can use for Pen and Paper RPG's. 我正在尝试编写一个简单的Character Generator ,我可以用它来制作Pen and Paper RPG。 I was thinking about storing all my information in a nested dictionary and saving it into a JSON file. 我正考虑将所有信息存储在嵌套字典中并将其保存到JSON文件中。

However, while creating the following dictionary, I receive as error: 但是,在创建以下字典时,我收到错误:

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

I am simply trying to create the dictionary, not calling any keys from it. 我只是想创建字典,而不是从它调用任何键。

As I understand from TypeError: unhashable type: 'dict' , I can use frozenset() to get keys. 据我所知,从TypeError:unhashable类型:'dict' ,我可以使用frozenset()来获取密钥。

Is there a better way to do what I am trying to do? 有没有更好的方法来做我想做的事情?

You seem to be making dictionaries {...} incorrectly for Python. 您似乎正在为Python错误地制作字典{...}

Lists look like this: 列表看起来像这样:

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

Dictionaries look like this: 字典看起来像这样:

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

If I'm guessing the behavior you want correctly, then you probably wanted something like this: 如果我正在猜测你想要的行为,那么你可能想要这样的东西:

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

The problem is not with the dict s, but with the set s. 问题不在于dict ,而在于set s。 The elements of a set must be hashable. set的元素必须是可以清除的。 In

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

the key is fine, but the value is an illegal set , because its elements are dict s. 关键是好的,但值是非法set ,因为它的元素是dict You could make frozenset s from the set s and you'd be OK. 你可以从set制作frozenset set ,你就可以了。

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

I think this : 我认为这 :

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

should be a list. 应该是一个清单。 Otherwise, each of the comma-separated elements is a mutable element which you are trying to store in a set. 否则,每个逗号分隔的元素都是您尝试存储在集合中的可变元素。 You are already doing it right with the very last entry: 您已使用最后一个条目正确执行此操作:

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

so why not all the others? 为什么不是所有其他人?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM