我想遍历列表,对于列表中的每个元素,我都必须遍历jinja2中列表的其他多个列表中的各个元素

时间:2019-09-19 07:34:46

标签: jinja2

我要遍历列表,对于列表的每个元素,我都必须遍历jinja2中其他列表的各个元素。

我尝试过

我尝试了以下Jinja2代码

{% for connected in connected_devices %}
  {% set connected_port = connected_devices_ports[loop.index-1] %}
  {% set old_port = old_device_ports[loop.index-1] %}
  {% set new_port = new_device_ports[loop.index-1] %}
    {{old_device}} {{old_port}} {{new_device}} {{new_port}} {{connected}} {{connected_port}} 
{% endfor %}

{% for connected in connected_devices %}
  {% for connected_port,old_port,new_port in connected_devices_ports[loop.index0], old_device_ports[loop.index0], new_device_ports[loop.index0]%}
    {{old_device}} {{old_port}} {{new_device}} {{new_port}} {{connected}} {{connected_port}} 
  {% endfor %}
{% endfor %}

Python变量

old_device="O1"

new_device = "N1"

connected_devices=["A1","B1","C1"]

connected_devices_ports=[["A1_port1","A1_port2"],["B1_port1","B1_port2"],["C1_port1","C2_port1"]]

old_device_ports=[["O1_to_A1_port1","O1_to_A1_port2"],["O1_to_B1_port1","O1_to_B1_port2"],["O1_to_C1_port1","O1_to_C1_port2"]]

new_device_ports=[["N1_to_A1_port1","N1_to_A1_port2"],["N1_to_B1_port1","N1_to_B1_port2"],["N1_to_C1_port1","N1_to_C1_port2"]]

预期产量

O1 O1_to_A1_port1 N1 N1_to_A1_port1 A1 A1_port1 O1 O1_to_A1_port2 N1 N1_to_A1_port2 A1 A1_port2 O1 O1_to_B1_port1 N1 N1_to_B1_port1 B1 B1_port1 O1 O1_to_B1_port2 N1 N1_to_B1_port2 B1 B1_port2 O1 O1_to_C1_port1 N1 N1_to_C1_port1 C1 C1_port1 O1 O1_to_C1_port2 N1 N1_to_C1_port2 C1 C1_port2

我尝试了以下Jinja2代码

{% for connected in connected_devices %}
  {% set connected_port = connected_devices_ports[loop.index-1] %}
  {% set old_port = old_device_ports[loop.index-1] %}
  {% set new_port = new_device_ports[loop.index-1] %}
    {{old_device}} {{old_port}} {{new_device}} {{new_port}} {{connected}} {{connected_port}} 
{% endfor %}

{% for connected in connected_devices %}
  {% for connected_port,old_port,new_port in connected_devices_ports[loop.index0], old_device_ports[loop.index0], new_device_ports[loop.index0]%}
    {{old_device}} {{old_port}} {{new_device}} {{new_port}} {{connected}} {{connected_port}} 
  {% endfor %}
{% endfor %}

预期产量

O1 O1_to_A1_port1 N1 N1_to_A1_port1 A1 A1_port1 O1 O1_to_A1_port2 N1 N1_to_A1_port2 A1 A1_port2 O1 O1_to_B1_port1 N1 N1_to_B1_port1 B1 B1_port1 O1 O1_to_B1_port2 N1 N1_to_B1_port2 B1 B1_port2 O1 O1_to_C1_port1 N1 N1_to_C1_port1 C1 C1_port1 O1 O1_to_C1_port2 N1 N1_to_C1_port2 C1 C1_port2

0 个答案:

没有答案