python中的字典的字典

dre*_*mer 1 python dictionary list

tags = {'stream', 'auth'} 
tags['stream']= {}
tags["stream"]["path"]= ["/streams"]
tags['stream']['attribute']= ['id', 'secure', 'cpcode', 'format', 'event_pattern']
Run Code Online (Sandbox Code Playgroud)

上面的代码会抛出一个错误:

    tags["stream"]= {} 
TypeError: 'set' object does not support item assignment
Run Code Online (Sandbox Code Playgroud)

如何创建列表字典的字典?

Mar*_*ers 5

您创建的是一个set,而不是字典。您需要指定键值对:

tags = {'stream': None, 'auth': None} 
Run Code Online (Sandbox Code Playgroud)

或者用文字符号就地指定嵌套字典:

tags = {
    'stream': {
        'path': ["/streams"],
        'attribute': ['id', 'secure', 'cpcode', 'format', 'event_pattern'],
    }, 
    'auth': None,
} 
Run Code Online (Sandbox Code Playgroud)

语法{value, value, value}(无键)是集合文字表示法。