[英]How to retrieve the value of a key in a dictionary using a variable defined in TAL?
I have defined a dictionary in python: 我在python中定义了一个字典:
tal:define="dic python:{'a':'1', 'b':'2'};"
I have another variable defined using tal:define
called var, which is either 'a' or 'b'. 我有另一个使用
tal:define
变量,称为var,它是'a'或'b'。 I get an error when I try to do: 尝试执行时出现错误:
tal:define="foo python:dic['${var}'];"
(foo would then be '1' or '2') I get KeyError: '${var}'
. (然后foo为'1'或'2'),我得到
KeyError: '${var}'
。 I've tried dic[$var]
, dic[var]
, etc and they all don't work. 我试过
dic[$var]
, dic[var]
等,但它们都不起作用。 How do I use a variable defined previously using tal:define
as a key to a dictionary to get its value. 如何使用先前使用
tal:define
的变量作为字典的键来获取其值。
DonCallisto的dic [var]确实有效
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.