gpt4 book ai didi

python - 如何检查mako dict中是否存在某个键?

转载 作者:行者123 更新时间:2023-12-01 05:37:19 25 4
gpt4 key购买 nike

如果我有以下情况:

mydict = {
a: 'A'
}

如何检查字典中是否存在键a?伪代码如下:

%if 'a' in mydict.keys()
${mydict['a']}
%endif

最佳答案

您可以只使用in:

from mako.template import Template
t = Template("""
% if key in d:
key is in dictionary
% else:
key is not in dictionary
% endif
""")


print t.render(key='a', d={'a': 'A'}) # prints "key is in dictionary"

关于python - 如何检查mako dict中是否存在某个键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18624290/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com