我有以下字典:
In [32]: mydict
Out[32]:
{'Foo': {'DendriticCells': {'LV.ip': [15.14,1.003],
'SP.ip': [16.0282,3.001]},
'Macrophages': {'LV.ip': [32.137260000000005],
'SP.ip': [34.020810000000004]},
'NKCells': {'LV.ip': [4.89852], 'SP.ip': [5.18562]}}}
给定一个对应于关键级别 3 的字符串,我想做些什么来构造一个
根据下面的 choices
检查字典中是否存在。
有什么办法呢。我试过了但是失败了。
choice1 = "LV.ip"
choice2 = "KK.ip"
choices = [choice1,choice2]
celltypes = ["DendriticCells", "Macrophages", "NKCells"]
for ch in choices:
for ct in celltypes:
if mydict["Foo"][ct][choices]:
print "THERE\n"
else:
print "Not there\n"
最佳答案
- 您的
if
语句应该使用ch
,而不是choices
- 你应该在
if
中分解测试;例如,在尝试查看它是否包含任何内容之前,请确保mydict["Foo"][ct]
存在。
关于python - 三层嵌套字典的存在性检验,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22677426/