Python 嵌套列表上的Jinja循环创建空输出
在Python 3.4中,我将此结构的af列表传递给名为Python 嵌套列表上的Jinja循环创建空输出,python,loops,jinja2,Python,Loops,Jinja2,在Python 3.4中,我将此结构的af列表传递给名为bar的模板: [{'var': 1.18, 'occurrences': [0.0805, 0.0808, 0.0991, 0.0994, 0.2356], 'name': 'item name'}, {'var': 2.31, 'occurrences': [1.0859, 1.1121, 1.4826, 1.4829, 1.8126, 1.8791], 'name': 'other name'}] 我希望它为每个字典创建以下输出:
bar
的模板:
[{'var': 1.18, 'occurrences': [0.0805, 0.0808, 0.0991, 0.0994, 0.2356], 'name': 'item name'},
{'var': 2.31, 'occurrences': [1.0859, 1.1121, 1.4826, 1.4829, 1.8126, 1.8791], 'name': 'other name'}]
我希望它为每个字典创建以下输出:
% List with names
item 1: item name
item 2: other name
% List with vars
item 1: 1.18
item 2: 2.31
% List with occurences
item 1: 0.0805, 0.0808, 0.0991, 0.0994, 0.2356
item 2: 1.0859, 1.1121, 1.4826, 1.4829, 1.8126, 1.8791
前两个没有问题,但我无法让它循环事件列表。我使用以下jinja模板:
{% for item in bars %}
item {{ loop.index }}: {{ item.name }}
{% endfor %}
{% for item in bars %}
item {{ loop.index }}: {{ item.var }}
{% endfor %}
{% for item in bars recursive %}
{% if item.occurrences %}
Item {{ loop.index}}: {{ loop(item.occurrences) }}
{% else %}
No content
{% endif %}
{% endfor %}
这在第三种情况下给出了奇数输出:
Item 1: No content
No content
No content
No content
No content
Item 2: No content
No content
No content
No content
No content
No content
这很奇怪,因为它似乎循环列表中的每一项,但它没有通过内容测试。我做错了什么
编辑:这三个答案都为我指明了正确的方向,但@famousgarkin给出了最详细、最灵活的答案。我最终得到了以下解决方案:
{% for item in bars %}
Item {{ loop.index }}: {% for occurrence in item.occurrences %} subitem {{ loop.index }}: {{ occurrence }} {% endfor %}
{% endfor %}
这使我能够在单独的上下文中包含每个项目,这正是我想要的。但由于这一目标从一开始就不明确,我希望我能投票否决你所有的答案。对不起,谢谢大家的快速帮助 把清单打印出来就行了
{% for item in bars %} #recursive not needed
{% if item.occurrences %}
Item {{ loop.index }}: {{ item.occurrences }} #we're not looping here, just printing out the list
{% endif %}
{% endfor %}
如果条
总是有出现
,则您甚至不需要If
检查
要从事件中打印单个项目,只需对其进行迭代
Item {{ loop.index}}: {% for occurrence in occurrences %}
{{ occurrence }}{% if not loop.last %}, {% endif %}
{% endfor %}
如果仍要打印整个列表,则可以使用一个过滤器,该过滤器返回列表的str表示形式,不带方括号。如果不想使用完全相同的逻辑格式化嵌套项,则不使用递归 在您的示例中,当循环
条
项时,它会转到正确的路径,因为您可以在输出上看到项n:
。然后它下降到一个递归调用来处理item.executions
items。现在,当它询问是否存在事件时,它实际上是在询问条.事件[i].事件[j]
,这当然没有事件
属性,而是进入无内容路径。您可以使用以下命令查看它的运行情况:
{% for item in bars recursive %}
{% if item.occurrences %}
Item {{ loop.index }}: {{ loop(item.occurrences) }}
{% else %}
No content, {{ item.__class__ }}, {{ item }}
{% endif %}
{% endfor %}
收益率:
Item 1: No content, <type 'float'>, 0.0805
No content, <type 'float'>, 0.0808
No content, <type 'float'>, 0.0991
No content, <type 'float'>, 0.0994
No content, <type 'float'>, 0.2356
...
Item 1: [0.0805, 0.0808, 0.0991, 0.0994, 0.2356]
Item 2: [1.0859, 1.1121, 1.4826, 1.4829, 1.8126, 1.8791]
Item 1: 0.0805, 0.0808, 0.0991, 0.0994, 0.2356
Item 2: 1.0859, 1.1121, 1.4826, 1.4829, 1.8126, 1.8791
Item 1:
1. 0.0805
2. 0.0808
3. 0.0991
4. 0.0994
5. 0.2356
...
收益率:
Item 1: No content, <type 'float'>, 0.0805
No content, <type 'float'>, 0.0808
No content, <type 'float'>, 0.0991
No content, <type 'float'>, 0.0994
No content, <type 'float'>, 0.2356
...
Item 1: [0.0805, 0.0808, 0.0991, 0.0994, 0.2356]
Item 2: [1.0859, 1.1121, 1.4826, 1.4829, 1.8126, 1.8791]
Item 1: 0.0805, 0.0808, 0.0991, 0.0994, 0.2356
Item 2: 1.0859, 1.1121, 1.4826, 1.4829, 1.8126, 1.8791
Item 1:
1. 0.0805
2. 0.0808
3. 0.0991
4. 0.0994
5. 0.2356
...
如果您希望自己迭代项目以提供自己的格式,例如,您可以使用过滤器:
收益率:
Item 1: No content, <type 'float'>, 0.0805
No content, <type 'float'>, 0.0808
No content, <type 'float'>, 0.0991
No content, <type 'float'>, 0.0994
No content, <type 'float'>, 0.2356
...
Item 1: [0.0805, 0.0808, 0.0991, 0.0994, 0.2356]
Item 2: [1.0859, 1.1121, 1.4826, 1.4829, 1.8126, 1.8791]
Item 1: 0.0805, 0.0808, 0.0991, 0.0994, 0.2356
Item 2: 1.0859, 1.1121, 1.4826, 1.4829, 1.8126, 1.8791
Item 1:
1. 0.0805
2. 0.0808
3. 0.0991
4. 0.0994
5. 0.2356
...
或再次循环以完全滚动自定义格式:
{% for item in bars %}
{% if item.occurrences %}
Item {{ loop.index }}:
{% for occurrence in item.occurrences %}
{{ loop.index }}. {{ occurrence }}
{% endfor %}
{% else %}
No content
{% endif %}
{% endfor %}
收益率:
Item 1: No content, <type 'float'>, 0.0805
No content, <type 'float'>, 0.0808
No content, <type 'float'>, 0.0991
No content, <type 'float'>, 0.0994
No content, <type 'float'>, 0.2356
...
Item 1: [0.0805, 0.0808, 0.0991, 0.0994, 0.2356]
Item 2: [1.0859, 1.1121, 1.4826, 1.4829, 1.8126, 1.8791]
Item 1: 0.0805, 0.0808, 0.0991, 0.0994, 0.2356
Item 2: 1.0859, 1.1121, 1.4826, 1.4829, 1.8126, 1.8791
Item 1:
1. 0.0805
2. 0.0808
3. 0.0991
4. 0.0994
5. 0.2356
...
这是工作代码
- 管理空白以按计划保留行
- 使用过滤器
join
创建事件列表
- 从循环引用中删除了
recursive
(因为数据是平面的且不是嵌套的)
运行此脚本:
from jinja2 import Template
bars = [{'var': 1.18, 'occurrences': [0.0805, 0.0808, 0.0991, 0.0994, 0.2356], 'name': 'item name'},
{'var': 2.31, 'occurrences': [1.0859, 1.1121, 1.4826, 1.4829, 1.8126, 1.8791], 'name': 'other name'}]
templ_str = """
% List with names
{% for item in bars -%}
item {{ loop.index }}: {{ item.name }}
{% endfor %}
% List with vars
{% for item in bars -%}
item {{ loop.index }}: {{ item.var }}
{% endfor %}
% List with occurrences
{%- for item in bars %}
item {{ loop.index}}:{{" "}}
{%- if item.occurrences -%}
{{ item.occurrences|join(", ") }}
{%- else -%}
No content
{%- endif -%}
{%- endfor -%}
"""
templ = Template(templ_str)
print templ.render(bars=bars)
您将获得以下输出:
% List with names
item 1: item name
item 2: other name
% List with vars
item 1: 1.18
item 2: 2.31
% List with occurrences
item 1: 0.0805, 0.0808, 0.0991, 0.0994, 0.2356
item 2: 1.0859, 1.1121, 1.4826, 1.4829, 1.8126, 1.8791
但是我该怎么做才能使它循环列表并打印每个项目,而不是只打印整个列表?顺便说一句,我误解了if语句,打算让它检查事件是否属实,但现在我发现它是错的。我不需要那个@vikki@Stenskjaer由于事件
是一个列表,您可以再次迭代,请参见我的编辑。