Python 我如何访问 Jinja2 中的部分列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4062226/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I access part of a list in Jinja2
提问by Jesse Spielman
I'm trying to use the jinja2 templating langauge to return the last n(say, 5) posts in my posts list:
我正在尝试使用 jinja2 模板语言来返回我的帖子列表中的最后 n(例如 5)个帖子:
{% for recent in site.posts|reverse|slice(5) %}
{% for post in recent %}
<li> <a href="/{{ post.url }}">{{ post.title }}</a></li>
{% endfor %}
{% endfor %}
This is returning the whole list though. How do you strip the first or last n elements?
不过,这将返回整个列表。你如何去除第一个或最后 n 个元素?
回答by Garrett
Try subscript notation, as in normal Python. For example, to take the last 5 posts and display them in reverse order:
尝试下标表示法,就像在普通 Python 中一样。例如,取最后 5 个帖子并以相反的顺序显示它们:
import jinja2
tmpl = """\
{%- for col in posts[-5:]|reverse|slice(3) -%}
{%- for post in col -%}
{{ post }}
{%- endfor -%}
<br>
{%- endfor -%}"""
jinja2.Template(tmpl).render(posts=[1,2,3,4,5,6,7])
produces: u'76<br>54<br>3<br>'
产生: u'76<br>54<br>3<br>'
回答by Rico Schiekel
this is a bit simpler I think without the use of the slicefilter:
我认为如果不使用切片过滤器,这会更简单一些:
{% for post in site.posts | reverse | list[0:4] %}
<li>» <a href="/{{ post.url }}">{{ post.title }}</a></li>
{% endfor %}
another way is to use the loop controls extension:
另一种方法是使用循环控制扩展:
{% for post in site.posts | reverse %}
{%- if loop.index > 4 %}{% break %}{% endif %}
<li>» <a href="/{{ post.url }}">{{ post.title }}</a></li>
{%- endfor %}
回答by Andrey Vlasovskikh
I came up with the following code:
我想出了以下代码:
{% for x in xs | batch(n) | first %}
...
{% endfor %}
The batch(n)filter splits a list xsinto sublists of length n, then the firstfilter selects the first of these sublists.
该batch(n)滤波器将列表xs入长度的子列表n,则该first过滤器选择第一这些子列表的。
回答by joemurphy
I had the same problem too. It's a simple answer. This retrieves the last five items in site.posts:
我也有同样的问题。这是一个简单的答案。这将检索 site.posts 中的最后五个项目:
{% for recent in site.posts[-5:] %}
{% for post in recent %}
<li> <a href="/{{ post.url }}">{{ post.title }}</a></li>
{% endfor %}
{% endfor %}
回答by Andrew
@Andrey's answer has the right idea. However, to fully solve your question:
@Andrey 的回答是正确的。但是,要完全解决您的问题:
{% for recent in site.posts|batch(5)|list|last|reverse %}
<li> <a href="/{{ recent.url }}">{{ recent.title }}</a></li>
{% endfor %}
Alternatively:
或者:
{% for recent in site.posts|reverse|batch(5)|first %}
<li> <a href="/{{ recent.url }}">{{ recent.title }}</a></li>
{% endfor %}
Whichever one you use depends on your preferences.
您使用哪一种取决于您的喜好。
回答by mattgately
For me, the following simple code works and doesn't require the whole chain of jinja filters. Simply use the list filter to convert to list and then do normal array slicing (note the parantheses):
对我来说,下面的简单代码有效,不需要整个 jinja 过滤器链。只需使用列表过滤器转换为列表,然后进行正常的数组切片(注意括号):
{% for recent in (site.posts | list)[-5:] %}
{% for post in recent %}
<li> <a href="/{{ post.url }}">{{ post.title }}</a></li>
{% endfor %}
{% endfor %}
I had the same problem, but my data was in a sequence rather than a list and this code handles both.
我有同样的问题,但我的数据是在一个序列中而不是一个列表中,这段代码同时处理了这两个问题。
回答by pratik soni
To get the last element, get total index from the array list.
要获取最后一个元素,请从数组列表中获取总索引。
For example, your object name is foundappointmentlog.
例如,您的对象名称是foundappointmentlog.
{% set total=foundappointmentlog|length %} //it return length
{{foundappointmentlog[total-1].appointment_result}} // here you get your last value using index

