문제

Say if I have the following:

mydict = {
   a: 'A'
}

How do I check if key a exists in the dictionary? Pseudocode below:

%if 'a' in mydict.keys()
  ${mydict['a']}
%endif
도움이 되었습니까?

해결책

You can just use 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"
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top