获取字典的特定部分



我需要得到字典的某一部分。我需要的部分是粗体的,我不确定如何才能达到这个(我需要在其他键上这样做,而这只是一个键)

{";完成下面的陈述:状态从——(1)——变为气体称为——(2)——。{"['1:液体;2:蒸发',1:液体';2:熔化’,1:固体;2:蒸发',1:固体';2:融化的]":"1:液体;2: evaporation",['1:液体;2:沉积’,1:液体’;2:升华’,1:固体;2:沉积’,1:固体’;2:升华'];1:固体;2: sublimation"},

如果你把这个字典写得更清楚一点,你可以看到这是一个包含一个字典的字典,也就是一个嵌套字典。

{" Complete the following statement: changing state from ---(1)--- to gas is known as ---(2)---.": 
{
"['1: liquid; 2: evaporation', '1: liquid; 2: melting', '1: solid; 2: evaporation', '1: solid; 2: melting']": "1: liquid; 2: evaporation",
"['1: liquid; 2: deposition', '1: liquid; 2: sublimation', '1: solid; 2: deposition', '1: solid; 2: sublimation']": "1: solid; 2: sublimation"
}
}

要访问以粗体标记的值,首先必须访问顶级字典中的嵌套字典。您可以通过d['keyname']d.get('keyname')执行此操作。

d = {
" Complete the following statement: changing state from ---(1)--- to gas is known as ---(2)---.": {
"['1: liquid; 2: evaporation', '1: liquid; 2: melting', '1: solid; 2: evaporation', '1: solid; 2: melting']": "1: liquid; 2: evaporation",
"['1: liquid; 2: deposition', '1: liquid; 2: sublimation', '1: solid; 2: deposition', '1: solid; 2: sublimation']": "1: solid; 2: sublimation"
}
}
nested_d = d.get(" Complete the following statement: changing state from ---(1)--- to gas is known as ---(2)---.")
print(nested_d.get("['1: liquid; 2: evaporation', '1: liquid; 2: melting', '1: solid; 2: evaporation', '1: solid; 2: melting']"))
# output: "1: liquid; 2: evaporation"

相关内容

  • 没有找到相关文章

最新更新