【问题标题】:Combine two lists and alternating the result in Jinja?合并两个列表并在 Jinja 中交替结果?
【发布时间】:2018-05-25 11:08:58
【问题描述】:

我正在开发沙盒 CMS,因此除了 Jinja 版本之外,我无法运行任何 Python 代码。

我正在从数据库中提取我的列表,并根据字段 = 某个值来拆分它们。

{# rows = command to pull from db #}

{% set row_one = rows|selectattr('resource','equalto','One') %}
{% set row_two = rows|selectattr('resource','equalto','Two') %}
{# Sets my empty array #}
{% set newobj = [] %}

从这里开始,控制台记录第一/第二行将导致仅向我显示适用于其各自资源类型的那些项目。

当我尝试将这两个填充到一个数组中然后迭代它以交替结果时,我的问题就出现了。

这是我尝试过的:

{% for row in rows %}
   {% newObj.update(row_one[loop.index0], row_two[loop.index0] %}
{% endfor %}

这似乎在newObj 上引发错误

未知标签'newobj'

我让它停止使用 :

{% for row in rows %}
  {% set objs = newObj.update(marketingRows[loop.index0], salesRows[loop.index0], designRows[loop.index0]) %}
{% endfor %}

但事实证明,当控制台记录 objs 时,这不会返回任何内容。


我想要的结果

{# example input #}
row_one = ['1', '2', '3', '4']
row_two = ['a', 'b', 'c', 'd']

{# desired output #}
objs =  ['1', 'a', '2', 'b', '3', 'c', '4', 'd']

我在这里完全不知所措,感谢任何帮助!

【问题讨论】:

  • row_onerow_two 是否总是等长?
  • 不,他们可以改变,但如果一行用完,它应该只是填补其余的

标签: jinja2


【解决方案1】:

根据个人经验,由于您无法运行任何 python 代码,因此在 jinja 中没有简单的方法来完成此操作。由于jinja不支持zip,而max直到v2.10才可用,所以我们需要即兴发挥。

首先,您需要获取任一列表的最长长度。

{%- set row_one = ['1', '2', '3', '4'] -%}
{%- set row_two = ['a', 'b', 'c', 'd'] -%}
{%- set rows_combined = (row_one, row_two) -%}

{%- set lengths = [] %}
{%- for row in rows_combined -%}{%- if lengths.append(row|length)-%}{%- endif -%}{%- endfor -%}
{%- set max_length = (lengths|sort)[-1] -%}

接下来,你需要做一个嵌套循环。首先遍历最大长度的范围,然后rows_combined 来获取row_onerow_two 的正确索引。

{%- set rows = [] -%}    

{# Loops through a range of max_length #}
{%- for r in range(max_length) -%}
    {# Loops through the tuple containing row_one and row_two #}
    {%- for a in rows_combined -%}
        {# checks if a[r] exists if based on current index, if so appends it rows#}
        {%- if a[r] -%}{%- if rows.append(a[r]) -%}{%- endif -%}{%- endif -%}
    {%- endfor -%}
{%- endfor -%}

{{ rows }}

>>>['1', 'a', '2', 'b', '3', 'c', '4', 'd']

使用 3 个列表而不是 2 个列表进行测试

{%- set row_one = ['1', '2', '3', '4'] -%}
{%- set row_two = ['a', 'b', 'c', 'd'] -%}
{%- set row_three = ['w', 'x', 'y', 'z'] -%}
{%- set rows_combined = (row_one, row_two, row_three) -%}

{%- set lengths = [] %}
{%- for row in rows_combined -%}{%- if lengths.append(row|length)-%}{%- endif -%}{%- endfor -%}
{%- set max_length = (lengths|sort)[-1] -%}
{%- set rows = [] -%}

{%- for r in range(max_length) -%}
    {%- for a in rows_combined -%}
        {%- if a[r] -%}{%- if rows.append(a[r]) -%}{%- endif -%}{%- endif -%}
    {%- endfor -%}
{%- endfor -%}
{{ rows }}

>>> ['1', 'a', 'w', '2', 'b', 'x', '3', 'c', 'y', '4', 'd', 'z']

【讨论】:

  • 哇,这真是令人难以置信的深思熟虑!这次真是万分感谢。我收到一个错误调用范围,所以我不确定它是否会在我们的 CMS 中运行:/,在我们说话的时候寻找解决方案。无论如何,这可能是公认的答案,因为如果有人不受沙箱的限制,它应该可以完美运行!
  • @ether 我不熟悉 CMS,但 range 是 jinja2 原生的,所以我想知道为什么它会给你错误。当我有机会时,我会看看我是否可以找到range的替代品
  • 我们的文档说它受支持,但它一直给我调用函数错误,感谢您对此的帮助!
猜你喜欢
  • 2014-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多