'my loop are confuse in multiple list difference size in flask
I want a result like:
1:
2:
3: 3 p3 33
4: 4 p4 44
....
...
But what I get is
1:
2:
3: 3 p3 33
3: 4 p4 44
3: 7 p7 77
...
This is my python code
str1 = [3,4,7,9]
n_save = ['p3','p4','p7','p9']
d_save = ['33','44','77','99']
zipped = zip(str1,n_save,d_save)
# flask code
{% for i in range(0,10) %}<br>
{% if i+1 in str1|list %}
{% for numm,namee,dd in zipped %}
<option value="{{ i+1 }}"> {{ i+1 }} : {{ namee }} {{ dd }}</option>
{% endfor %}
{% else %}
<option value="{{ i+1 }}"> {{ i+1 }} : </option>
{% endif %}
{% endfor %}
Any help appreciated.
Solution 1:[1]
In the example below, the iterator is first converted into a list.
A pointer to the current index of the zipped list is then created using a namespace.
Now the respective entry of the list can be unpacked.
{% set data = zipped | list -%}
{% set ctx = namespace(index0=0) -%}
{% for i in range(1,11) -%}
{% if ctx.index0 < (data | length) and i == data[ctx.index0][0] -%}
{% set _, namee, dd = data[ctx.index0] -%}
<option value="{{ i }}">{{ i }} : {{ namee }} {{ dd }}</option>
{% set ctx.index0 = ctx.index0 + 1 -%}
{% else -%}
<option value="{{ i }}">{{ i }} : </option>
{% endif -%}
{% endfor -%}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
