唯一过滤器似乎不适用于连接的列表

huangapple go评论78阅读模式
英文:

Unique filter does not seems to apply on concatenated list

问题

我有两个列表,我想将它们连接起来,然后仅筛选出唯一的值。我认为这应该在Jinja2中使用unique过滤器来实现,但是我发现它并不像我期望的那样工作。我已经使用了一些不同的在线Jinja解析器进行测试,它们似乎都得到了不正确的输出。

供参考,我用于测试我的代码的网站是https://j2live.ttl255.com/

使用以下数据输入:

this:
  - one
  - one
that:
  - one
  - two

和以下模板:

{{ this + that | unique | list }}

渲染输出只是将这两个列表连接起来的结果,尽管它们明显具有重复的值:

['one', 'one', 'one', 'two']

我尝试了很多不同的排列组合,它们最终都以相同的方式结束。有没有人知道如何使其按预期工作?或者我漏掉了什么?根据过滤器的意图,我认为这应该可以工作。

我还想指出,当列表未连接时,预期的行为确实发生了。例如:

{{ this | unique | list }}

的结果是:

['one']
英文:

I have two lists that I want to concatenate and then filter for unique values only. I figured this should be possible in Jinja2 using the unique filter, but, I'm finding it doesn't work the way I expected. I've used a few different live Jinja parsers to test and they all seem to end up with incorrect output.

For reference, the site I'm using to test my code is https://j2live.ttl255.com/

With an input of data that looks like this:

this:
  - one
  - one
that:
  - one
  - two

And a template that looks like this:

{{ this + that | unique | list }}

The rendered output is just the result of concatenating the two lists, when they clearly have duplicate values:

['one', 'one', 'one', 'two']

I've tried a bunch of different permutations here and they all end up the same way. Does anyone know how to get this to work as expected? Or am I missing something here? With the intent of the filter, I figured this would work.

I also want to point out, that the expected behaviour does occur when the lists are not concatenated. For example:

{{ this | unique | list }}

Results in:

['one']

答案1

得分: 2

代码中的翻译部分:

"precedence is taken by the filter over the union of the two list." 翻译为 "过滤器的优先级高于两个列表的并集。"

"What your code is effectively doing is:" 翻译为 "你的代码实际上是在做如下操作:"

"So, what you really want to do is for the union to happen first, which you can achieve by using parenthesis:" 翻译为 "所以,你真正想要的是首先进行并集操作,你可以通过使用括号来实现:"

"Which results in your expected:" 翻译为 "这将得到你期望的结果:"

"This is backed up by this comment on their issue tracker:" 翻译为 "这得到了他们问题追踪器上的评论的支持:"

"Filters and tests have higher precedence than binary operators. For example {{ [3] + [2,1] | sort }} yields [3, 1, 2], not [1, 2, 3]." 翻译为 "过滤器和测试的优先级高于二元运算符。例如 {{ [3] + [2,1] | sort }} 会产生 [3, 1, 2],而不是 [1, 2, 3]。"

"As commented by ngaya-ll" 翻译为 "如ngaya-ll所评论的"

英文:

The precedence is taken by the filter over the union of the two list.
What your code is effectively doing is:

{{ this + (that | unique | list) }}

So, what you really want to do is for the union to happen first, which you can achieve by using parenthesis:

{{ (this + that) | unique | list }}

Which results in your expected:

['one', 'two']

This is backed up by this comment on their issue tracker:

> Filters and tests have higher precedence than binary operators. For example {{ [3] + [2,1] | sort }} yields [3, 1, 2], not [1, 2, 3].

<sup>As commented by ngaya-ll</sup>

huangapple
  • 本文由 发表于 2023年6月8日 03:01:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76426337.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定