Python中的嵌套字典
场景:根据我前面的问题(),我现在正试图以更高效的方式创建词典。我不是一次把所有的信息输入字典,而是一步一步地传递 到目前为止我拥有的:Python中的嵌套字典,python,dictionary,Python,Dictionary,场景:根据我前面的问题(),我现在正试图以更高效的方式创建词典。我不是一次把所有的信息输入字典,而是一步一步地传递 到目前为止我拥有的: cldr["holidays"] = {"type": "object", "description": "Holiday specification", "properties": { "default":{
cldr["holidays"] = {"type": "object",
"description": "Holiday specification",
"properties": {
"default":{
"type": "object",
"description": "Calendars used",
"properties":{
"ref": {"type": "string"},
"type": {"type": "string"},
"value": {"type": "string"}
}
},
"exante": {
"type": "object",
"description": "Calendars used",
"properties":{
"ref": {"type": "string"},
"type": {"type": "string"},
"value": {"type": "string"}
}
},
"expost": {
"type": "object",
"description": "Calendars used",
"properties":{
"ref": {"type": "string"},
"type": {"type": "string"},
"value": {"type": "string"}
}
},
}
}
cldr["holidays"] = {"type": "object",
"description": "Holiday specification",
"properties": {"default", "exante", "expost"}
}
cldr["holidays"]["properties"]["default"] = {
"type": "object",
"description": "",
"properties":{
"ref": {"type": "string"},
"type": {"type": "string"},
"value": {"type": "string"}
}
}
cldr["holidays"]["properties"]["exante"] = {
"type": "object",
"description": "",
"properties":{
"ref": {"type": "string"},
"type": {"type": "string"},
"value": {"type": "string"}
}
}
cldr["holidays"]["properties"]["expost"] = {
"type": "object",
"description": "",
"properties":{
"ref": {"type": "string"},
"type": {"type": "string"},
"value": {"type": "string"}
}
}
我想做什么:
cldr["holidays"] = {"type": "object",
"description": "Holiday specification",
"properties": {
"default":{
"type": "object",
"description": "Calendars used",
"properties":{
"ref": {"type": "string"},
"type": {"type": "string"},
"value": {"type": "string"}
}
},
"exante": {
"type": "object",
"description": "Calendars used",
"properties":{
"ref": {"type": "string"},
"type": {"type": "string"},
"value": {"type": "string"}
}
},
"expost": {
"type": "object",
"description": "Calendars used",
"properties":{
"ref": {"type": "string"},
"type": {"type": "string"},
"value": {"type": "string"}
}
},
}
}
cldr["holidays"] = {"type": "object",
"description": "Holiday specification",
"properties": {"default", "exante", "expost"}
}
cldr["holidays"]["properties"]["default"] = {
"type": "object",
"description": "",
"properties":{
"ref": {"type": "string"},
"type": {"type": "string"},
"value": {"type": "string"}
}
}
cldr["holidays"]["properties"]["exante"] = {
"type": "object",
"description": "",
"properties":{
"ref": {"type": "string"},
"type": {"type": "string"},
"value": {"type": "string"}
}
}
cldr["holidays"]["properties"]["expost"] = {
"type": "object",
"description": "",
"properties":{
"ref": {"type": "string"},
"type": {"type": "string"},
"value": {"type": "string"}
}
}
但这会产生以下错误:
TypeError: 'set' object does not support item assignment
问题1:对我做错了什么有什么看法吗
问题2:是否可以为本词典的内部部分创建一个共享类?既然它们本质上是相同的,我需要分别定义它们还是有更有效的方法来定义它们?这有什么不对吗? 您错误地在属性键处创建了一个集合
cldr["holidays"] = {"type": "object",
"description": "Holiday specification",
"properties": {"default", "exante", "expost"} # creates a set
}
cldr["holidays"]["properties"]["default"] = {
"type": "object",
"description": "",
"properties":{
"ref": {"type": "string"},
"type": {"type": "string"},
"value": {"type": "string"}
}
}
cldr["holidays"]["properties"]["exante"] = {
"type": "object",
"description": "",
"properties":{
"ref": {"type": "string"},
"type": {"type": "string"},
"value": {"type": "string"}
}
}
cldr["holidays"]["properties"]["expost"] = {
"type": "object",
"description": "",
"properties":{
"ref": {"type": "string"},
"type": {"type": "string"},
"value": {"type": "string"}
}
}
下面是您如何更正的方法
def holiday_property():
return {
"type": "object",
"description": "",
"properties": {
"ref": {"type": "string"},
"type": {"type": "string"},
"value": {"type": "string"}
}
}
cldr["holidays"] = {
"type": "object",
"description": "Holiday specification",
"properties": {}
}
cldr["holidays"]["properties"]["default"] = holiday_property()
cldr["holidays"]["properties"]["exante"] = holiday_property()
cldr["holidays"]["properties"]["expost"] = holiday_property()
为属性键指定一个空字典的值。当您执行cldr[“holidays”][“properties”][“default”]={the internal dictionary}
时,您将设置default
键及其值
cldr["holidays"] = {"type": "object",
"description": "Holiday specification",
"properties": {} # creates an empty dictionary
}
cldr["holidays"]["properties"]["default"] = {
"type": "object",
"description": "",
"properties":{
"ref": {"type": "string"},
"type": {"type": "string"},
"value": {"type": "string"}
}
}
cldr["holidays"]["properties"]["exante"] = {
"type": "object",
"description": "",
"properties":{
"ref": {"type": "string"},
"type": {"type": "string"},
"value": {"type": "string"}
}
}
cldr["holidays"]["properties"]["expost"] = {
"type": "object",
"description": "",
"properties":{
"ref": {"type": "string"},
"type": {"type": "string"},
"value": {"type": "string"}
}
}
听起来你想要什么
def holiday_property():
return {
"type": "object",
"description": "",
"properties": {
"ref": {"type": "string"},
"type": {"type": "string"},
"value": {"type": "string"}
}
}
cldr["holidays"] = {
"type": "object",
"description": "Holiday specification",
"properties": {}
}
cldr["holidays"]["properties"]["default"] = holiday_property()
cldr["holidays"]["properties"]["exante"] = holiday_property()
cldr["holidays"]["properties"]["expost"] = holiday_property()
错误消息是不言自明的<代码>{“default”、“exante”、“expost”}是一个
集合
,而集合
≠ <代码>指令。是什么让你觉得这样会更有效率?你做过任何基准测试吗?这将是一个dict{“default”:None,“exante”:None,“expost”:None}@meowgoes。我的问题是,将所有东西同时放在一起非常复杂(IMO),我最终会犯错误。因此,我试图将其分解为更小的部分。@KennyOstrom因此,在本例中,只需将每个内部部分设置为“无”,即可避免成为set
类型?只需执行“属性”:{},它将默认为一个空的dict。无论如何,您稍后将填充键。