本文介绍了如何从不同的索引启动forloop.counter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有2个独立的forloops,并且我在bothloops中都使用forloop.counter。我想从第一个forloop的结尾开始第二个forloop计数器
I have 2 seperate forloops and i am using forloop.counter in bothloops. I want to start the second forloop counter from the ending of first forloop
{% for i in something1 %}
<tr>
<td>{{ forloop.counter }}</td>
<td>i.username</td>
</tr>
{% endfor %}
{% for j in something2 %}
<tr>
<td>{{ forloop.counter }}</td>
<td>j.username</td>
</tr>
{% endfor %}
如果第一个forloop结束于10,那么我想开始来自11.plz帮助的下一个for循环
if the first forloop ends at 10 then i want to start the next for loop from 11.plz help
推荐答案
我对Django不太满意,因此在给出集合的普通Python:
I'm not comfortable with Django, so I show a couple of option in plain Python, given the collections:
something1 = [1,2,3,4]
something2 = [1,2,3,4,5,6,7,8,9,10]
您可以按索引访问对象(与数据库索引不同):
You can access objects by index (not the same as database index):
i = 1
for e1 in something1:
print(e1)
i += 1
for i2 in range(i,len(something2)):
print(something2[i2])
或切片最后一个集合:
for e1 in something1:
print(e1)
for e2 in something2[len(something1):-1]:
print(e2)
当然,最后一个集合必须是最长的。
Of course, the last collection has to be the longest.
这篇关于如何从不同的索引启动forloop.counter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!