将两个列表与字典键相关

时间:2019-08-22 07:55:25

标签: python html python-3.x

我正在尝试将两个列表与同一个字典键相关联;然后在html表中显示列表。我不知道是否选择了一种错误的方法来存储列表,或者是否弄乱了显示列表所需的for循环的配置。想知道是否有人可以提供帮助。

到目前为止我所拥有的:

我想联系的是:KeyA 两个列表:ListA和ListB。

存储:

listA = [1,2,3,4]
listB = [A,B]
keys = {}
lists = []
lists.append([ListA,ListB])
keys.setdefault(KeyA, []).append(lists) 

显示:

{% for option in keys %}
    <tr class = 'collapse level1' data-depth="1" >
        <td class ='site_device'> </td>
        <td class = 'site_device' > {{option}}</td>
        <td class ='site_device'></td>
        <td class ='site_device'></td>

        {% for list_holder in keys[option] %}
            {% for lists in list_holder %}
                {% for v1 in lists %}
                    <tr class = 'collapse level2' data-depth="2" >
                        <td class ='site_device'></td>
                        <td class ='site_device'></td>
                        <td class ='site_device'>{{ v1 }}</td>
                        <td class ='site_device'></td>
                    </tr>
                {% endfor %}
            {% endfor %}
        {% endfor %}
    </tr>
{% endfor %}

此方法给出:

<tr class = 'collapse level1' data-depth="1" >
    <td class ='site_device'> </td>
    <td class = 'site_device' > KeyA</td>
    <td class ='site_device'></td>
    <td class ='site_device'></td>
    <tr class = 'collapse level2' data-depth="2" >
        <td class ='site_device'></td>
        <td class ='site_device'></td>
        <td class ='site_device'>[1, 2, 3, 4]</td>
        <td class ='site_device'></td>
    </tr>
    <tr class = 'collapse level2' data-depth="2" >
        <td class ='site_device'></td>
        <td class ='site_device'></td>
        <td class ='site_device'>['A','B']</td>
        <td class ='site_device'></td>
    </tr>
</tr>

我要针对的目标:

<tr class = 'collapse level1' data-depth="1" >
    <td class ='site_device'> </td>
    <td class = 'site_device' > KeyA</td>
    <td class ='site_device'></td>
    <td class ='site_device'></td>
    <tr class = 'collapse level2' data-depth="2" >
        <td class ='site_device'></td>
        <td class ='site_device'></td>
        <td class ='site_device'> 1 </td>
        <td class ='site_device'> A </td>
    </tr>
    <tr class = 'collapse level2' data-depth="2" >
        <td class ='site_device'></td>
        <td class ='site_device'></td>
        <td class ='site_device'> 2 </td>
        <td class ='site_device'> B </td>
    </tr>
    <tr class = 'collapse level2' data-depth="2" >
        <td class ='site_device'></td>
        <td class ='site_device'></td>
        <td class ='site_device'> 3 </td>
        <td class ='site_device'></td>
    </tr>
    <tr class = 'collapse level2' data-depth="2" >
        <td class ='site_device'></td>
        <td class ='site_device'></td>
        <td class ='site_device'> 4 </td>
        <td class ='site_device'></td>
    </tr>
</tr>

2 个答案:

答案 0 :(得分:1)

append向列表中添加了一个新元素,因此最终得到一个三级列表(lists[[[1, 2, 3, 4], ['A', 'B']]]而不是[[1, 2, 3, 4], ['A', 'B']]

尝试使用extend代替:

lists.extend([ListA,ListB])

(或选择一一追加)

lists.append(ListA)
lists.append(ListB)

答案 1 :(得分:1)

我没有测试它,但是您将列表嵌入了一个级别。

因此,替换

lists.append([listA,listB]) 

通过

lists.append(listA)
lists.append(listB)

还有什么,为什么要使用它?:

keys.setdefault(KeyA, []).append(lists)

代替

keys.setdefault(KeyA, lists)
# or even
keys['KeyA']=lists

更容易阅读。

对于另一个问题,可能会出现类似这样的循环:


{% for option in keys %}
    <tr class = 'collapse level1' data-depth="1" >
        <td class ='site_device'> </td>
        <td class = 'site_device' > {{option}}</td>
        <td class ='site_device'></td>
        <td class ='site_device'></td>
        {% for list_holder in keys[option] %}
            {% for i in range(list_holder[0]|length) %}
                    <tr class = 'collapse level2' data-depth="2" >
                        <td class ='site_device'></td>
                        <td class ='site_device'></td>
                        <td class ='site_device'>{{ list_holder[0][i] }}</td>
                        <td class ='site_device'>{{ list_holder[1][i] }}</td>
                    </tr>
            {% endfor %}
        {% endfor %}
    {% endfor %}

当然,由于两个列表的大小都不相同,因此会引发IndexError(s)。因此,请确保两个列表的大小相同。