Python 金贾的列表理解
我有两份清单:Python 金贾的列表理解,python,jinja2,Python,Jinja2,我有两份清单: strainInfo,其中包含一个名为“replicateID”的字典元素 selectedStrainInfo,其中包含一个名为“replicateID”的字典元素 我希望检查我的每个菌株的replicateID是否在所选菌株的列表中,在python中,它是这样的: for strain in strainInfo: if strain.replicateID in [selectedStrain.replicateID for selectedStrain in se
for strain in strainInfo:
if strain.replicateID in [selectedStrain.replicateID for selectedStrain in selectedStrainInfo]
print('This strain is selected')
我在django中获得了正确的功能,但我想知道是否有一种方法可以简化使用列表理解:
{% for row in strainInfo %}
{% for selectedStrain in selectedStrainsInfo %}
{% if row.replicateID == selectedStrain.replicateID %}
checked
{% endif %}
{% endfor %}
{% endfor %}
您可以通过Jinja将数据传递给JavaScript变量,如下所示
var strainInfo = {{strainInfo|safe}};
var selectedStrainInfo = {{selectedStrainInfo|safe}};
然后在那里打扫卫生
使用Jinja的
safe
过滤器防止数据被HTML转义。自v2.7以来,存在selectattr
:
通过对每个对象的指定属性应用测试并仅在测试成功时选择对象,过滤对象序列
如果未指定测试,则属性的值将作为布尔值进行计算
用法示例:
{{ users|selectattr("is_active") }}
{{ users|selectattr("email", "none") }}
与发电机类似,例如:
(u for user in users if user.is_active)
(u for user in users if test_none(user.email))
看