【发布时间】:2017-11-07 21:29:51
【问题描述】:
我有一个主题列表:
list1 = [topic1, topic2, topic3, topic4, topic5, topic6]
我想对照此列表检查另一个列表:
list2 = [topic2, topic4, topic6]
类似这样的:
{% if list2.items in list1 %}
在 list1 中检查 list2 中的每个项目。如果 list2 中的所有或任何项目都在列表 1 中,则为 True。我认为这很简单,但我找不到任何有用的东西。
完整示例:
{% set list1 = [topic2, topic4, topic6] %}
{% for post in posts %}
{% set list2 = [topic1, topic2, topic3, topic4, topic5, topic6] %}
{% for topic in list2 %}
{% if topic in list1 %}
{# output of post list based on conditions #}
{% endif %}
{% endfor %}
{% endfor %}
** 我在没有服务器端访问的 cms 中工作,所以我只有模板语言可以使用。
【问题讨论】:
-
您可以使用列表理解:
[item for item in list1 if item in list2]
标签: python list compare jinja2 items