合并两个列表,并在Jinja中交替显示结果? [英] Combine two lists and alternating the result in Jinja?

查看:56
本文介绍了合并两个列表,并在Jinja中交替显示结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用沙盒CMS,因此除Jinja版本外,我无法运行任何pythonic代码.

I'm working on a sandboxed CMS so I can't run any pythonic code, other than versions of Jinja.

我正在从数据库中提取列表,然后根据其中的字段=某个特定值对列表进行拆分.

I am pulling my lists from a database where I split them based on having a field = a certain value.

{# 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 = [] %}

从这里开始,控制台记录第一行或第二行将只显示适用于其各自资源类型的那些项.

From here, console logging either Row one/two will result in showing me only those items that apply to their respective resource type.

当我尝试将这两个填充到数组中然后对其进行迭代以交替显示结果时,我的问题就出现了.

My problem comes when I try to stuff those two in an array and then iterate over it to alternate the result.

这是我尝试过的:

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

这似乎在newObj

未知标签"newobj"

Unknown tag 'newobj'

我已停止使用以下命令抛出错误:

I had it stop throwing the error by using :

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

但是事实证明,在控制台记录objs时,此操作什么也不返回.

But this has proved to return nothing when console logging 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']

我在这里无所适从,不胜感激!

I'm at a total loss here, any help is appreciated !

推荐答案

根据个人经验,并且由于您无法运行任何python代码,因此在Jinja中没有 easy 方式可以完成此操作.由于jinja不支持zip,并且max v2之前不可用. 10 ,所以我们需要即兴创作.

From personal experience, and since you are not able to run any python code, there is no easy way to accomplish this in jinja. Since jinja does not support zip, and max is not available until v2.10, so we need to improvise.

首先,您需要获得两个列表中最长的一个.

First, you need to get the longest length of either list.

{%- 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.

Next, you need to do a nested loop. First iterating through the range of the max length and then rows_combined to grab the correct index of row_one and row_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']

这篇关于合并两个列表,并在Jinja中交替显示结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆