Nis*_*ngh 12 python dictionary
我有一个简单的条件,我需要检查一个dict值是否包含[Complted]特定键中的说法.
例如:
'Events': [
{
'Code': 'instance-reboot'|'system-reboot'|'system-maintenance'|'instance-retirement'|'instance-stop',
'Description': 'string',
'NotBefore': datetime(2015, 1, 1),
'NotAfter': datetime(2015, 1, 1)
},
],
Run Code Online (Sandbox Code Playgroud)
我需要在启动时检查Description密钥是否包含[Complted]在其中.即
'Descripton':'[已完成]实例正在降级硬件上运行'
我怎么能这样做?我正在寻找类似的东西
if inst ['Events'][0]['Code'] == "instance-stop":
if inst ['Events'][0]['Description'] consists '[Completed]":
print "Nothing to do here"
Run Code Online (Sandbox Code Playgroud)
这应该有效。你应该使用in而不是consists. consistspython中没有任何东西被调用。
"ab" in "abc"
#=> True
"abxyz" in "abcdf"
#=> False
Run Code Online (Sandbox Code Playgroud)
所以在你的代码中:
if inst['Events'][0]['Code'] == "instance-stop":
if '[Completed]' in inst['Events'][0]['Description']
# the string [Completed] is present
print "Nothing to do here"
Run Code Online (Sandbox Code Playgroud)
希望能帮助到你 : )