Django模板-如何同时迭代两个字典 [英] Django template - How do i iterate two dictionary at same time

查看:320
本文介绍了Django模板-如何同时迭代两个字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Django的新手。我如何同时遍历2个字典,例如:

I am new to Django. How do i iterate over 2 dictionary at same time, example:

test1 = {'x':20,'y':10}  test2 = {'x':15,'y':5}  

我尝试过的键

 {% for k,v in test.iteritems %}
    <td >{{ k }} <td>
    <td >{{ v }} <td>
    <td >{{ test2[k] }} <td>
 {% endfor %}

test2 [k] 给我错误。我正在寻找任何其他方法来在test1循环内访问test2的值。

test2[k] giving me error. I am looking for any other way to access test2's values inside test1 loop.

推荐答案

将您的字典合并到列表容器中:

Combine your dictionaries into a list container:

tests = [test1, test2]

...在这种情况下,您可以像这样轻松地在模板中进行迭代:

...in which case, you can easily iterate in the template like so:

{% for test in tests %}
    <tr>
        <td> {{ test.x }} </td>
        <td> {{ test.y }} </td>
    </tr>
{% endfor %}






应该如果您希望它们在模板中处于相同的循环/行中,则可以创建一个元组列表容器,例如:


Should you want them in the same loop/row in the template, you can make a list container of tuples, e.g.:

tests = [(test1, test2), ]

...

{% for test_tuple in tests %}
    <tr>
        <td> {{ test_tuple.0.x }} </td>
        <td> {{ test_tuple.0.y }} </td>
        <td> {{ test_tuple.1.x }} </td>
        <td> {{ test_tuple.1.y }} </td>
    </tr>
{% endfor %}






编辑每个注释,要求在模板中输入关键字名称

每个表行进行一次测试:

With one test per table row:

{% for test in tests %}
    <tr>
    {% for key, value in test.items %}
        <td> {{ key }} = {{ value }} </td>
    {% endfor %}
    </tr>
{% endfor %}

或每个表行有多个测试:

or with multiple tests per table row:

{% for test_tuple in tests %}
    <tr>
    {% for test in test_tuple %}
        {% for key, value in test.items %}
            <td> {{ key }} = {{ value }} </td>
        {% endfor %}
    {% endfor %}
    </tr>
{% endfor %}

如果测试具有相同的键,则以上可能会含糊(例如x = 2,y = 3,x = 4,y = 2,x = 3,y = 5)...以下标形式显示索引的另一种选择:

The above may be ambiguous if the tests have the same key(e.g. x=2, y=3, x=4, y=2, x=3, y=5)... an alternative with the index printed as a subscript:

{% for test_tuple in tests %}
    <tr>
    {% for in test_tuple.count %}
        {% for key, value in test.items %}
            <td> {{ key }}<sub>{{ forloop.parentloop.counter }}</sub> = {{ value }} </td>
        {% endfor %}
    {% endfor %}
    </tr>
{% endfor %}

这将打印x1,y1,x2,y2等。

This would print x1, y1, x2, y2, etc.

再次编辑RE:在同一单元格或行中以行内/行显示多个x值,y值

在这种情况下,我将在将数据发送到模板之前操纵数据结构。

In this case, I would manipulate the data structures BEFORE sending data to the template.

I将创建x值列表和y值列表:

I would create a list of x values and a list of y values:

tests = [test1, test2]
x_values = map(lambda test: test['x'], tests)
y_values = map(lambda test: test['y'], tests)

...,然后确保将x_values和y_values发送到模板。

...and then make sure you send x_values and y_values to the template.

如果要如果是动态的,您可以尝试自省测试字典中的参数(例如 x或 y或运行前未知的内容)...

If you want it to be dynamically, you could try to introspect what the parameters are in the test dictionaries (e.g. 'x' or 'y' or something unknown before run-time)...

tests = [test1, test2, test3]
parameters = reduce(lambda accumulator, test: accumulator |= set(test.keys()), tests, set())

此代码设置参数的初始值又称为累加器等于 set()然后使用联合运算符 | = 将任何测试对象中的所有键追加到集合中...最终结果是您拥有一个看起来像 set([[x','y'])该参数仅计数一次。参数的动态自省很棘手,因此如果您 $ x_values y_values >知道,您只使用两个变量。

This code sets the initial value of parameters AKA accumulator equal to set() then uses the union operator |= to append all keys in any test object to the set... the end result is that you have a set looking like set(['x', 'y']) where the parameter is only counted once. Dynamic introspection of parameters is tricky, so you may want to stick to the simpler x_values and y_values above if you know you are working with only two variables.

接下来,为每个参数创建一个值列表:

Next, you create a list of values for each parameter:

# create a dictionary with keys X, Y and values list(x1,x2,x3), list(y1,y2,y3)
values_dictionary = dict()
# iterate through parameters X, Y, Z, ...
for parameter in parameters:
    # retrieve the parameter value IF IT EXISTS or else append None to new list
    values_for_parameter = map(lambda test: test.get(parameter, None), tests)
    # remove the None values from the resultant list
    values_dictionary[parameter] = filter(lambda value: value is not None, values_for_parameter)

# sample result: values_dictionary['x'] = [1, 5, 8, 4, 2, 3]

现在您有了更多有用的数据结构可在t中进行渲染他的模板。例如:

Now you have much more useful data structures for rendering in the template. For example:

{% for parameter, values in values_dictionary.items %}
    <tr>
        <td> {{ parameter }} </td>
        <td>
            {% for value in values %}
                {{ value }}
                {% if not forloop.lastloop %}, {% endif %}
            {% endfor %}
        </td>
    </tr>
{% endfor %}

这篇关于Django模板-如何同时迭代两个字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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