访问有序默认字典中的对象
Access objects in ordered default dict
我正在使用来自 here 的订单默认字典。问题是我不知道如何访问这些对象。
我希望像这样的东西能起作用
{% for zone in data %}
{% for reservation in zone %}
{{reservation}} # 1 | 2| 3
{% endfor %}
{% endfor %}
帮助调试的数据
{{data}}
OrderedDefaultDict(<type 'list'>, DefaultOrderedDict([('1', [<app.backoffice.models.Reservation object at 0x7f91c2c5ee10>, <app.backoffice.models.Reservation object at 0x7f91c2c732d0>, <app.backoffice.models.Reservation object at 0x7f91c2c73510>]), ('2', [<app.backoffice.models.Reservation object at 0x7f91c2c73790>, <app.backoffice.models.Reservation object at 0x7f91c32f9c50>]), ('3', [<app.backoffice.models.Reservation object at 0x7f91c2c733d0>, <app.backoffice.models.Reservation object at 0x7f91c2c73490>])]))
{% for zone in data %}
{{zone}} # 1 | 2 | 3
{{zone[0]}} # 1 | 2 | 3
{% endfor %}
当你遍历字典(甚至是子类)时,你会得到 keys;如果要遍历嵌套对象,则必须先将该键转换为值:
{% for zone in data %}
{% for reservation in data[zone] %}
{{reservation}}
{% endfor %}
{% endfor %}
由于您不显示 zone
键,您也可以遍历字典值(使用 dict.itervalues()
以避免创建冗余列表对象):
{% for reservations in data.itervalues() %}
{% for reservation in reservations %}
{{reservation}}
{% endfor %}
{% endfor %}
或使用dict.iteritems()
获取键和值:
{% for zone, reservations in data.iteritems() %}
{{zone}}:
{% for reservation in reservations %}
{{reservation}}
{% endfor %}
{% endfor %}
在您自己的尝试中,zone
仅设置到每个键,在您的情况下是单字符字符串('1'
、'2'
和 '3'
).遍历单个字符的字符串或使用 zone[0]
索引该字符串只会导致显示该字符。
我正在使用来自 here 的订单默认字典。问题是我不知道如何访问这些对象。
我希望像这样的东西能起作用
{% for zone in data %}
{% for reservation in zone %}
{{reservation}} # 1 | 2| 3
{% endfor %}
{% endfor %}
帮助调试的数据
{{data}}
OrderedDefaultDict(<type 'list'>, DefaultOrderedDict([('1', [<app.backoffice.models.Reservation object at 0x7f91c2c5ee10>, <app.backoffice.models.Reservation object at 0x7f91c2c732d0>, <app.backoffice.models.Reservation object at 0x7f91c2c73510>]), ('2', [<app.backoffice.models.Reservation object at 0x7f91c2c73790>, <app.backoffice.models.Reservation object at 0x7f91c32f9c50>]), ('3', [<app.backoffice.models.Reservation object at 0x7f91c2c733d0>, <app.backoffice.models.Reservation object at 0x7f91c2c73490>])]))
{% for zone in data %}
{{zone}} # 1 | 2 | 3
{{zone[0]}} # 1 | 2 | 3
{% endfor %}
当你遍历字典(甚至是子类)时,你会得到 keys;如果要遍历嵌套对象,则必须先将该键转换为值:
{% for zone in data %}
{% for reservation in data[zone] %}
{{reservation}}
{% endfor %}
{% endfor %}
由于您不显示 zone
键,您也可以遍历字典值(使用 dict.itervalues()
以避免创建冗余列表对象):
{% for reservations in data.itervalues() %}
{% for reservation in reservations %}
{{reservation}}
{% endfor %}
{% endfor %}
或使用dict.iteritems()
获取键和值:
{% for zone, reservations in data.iteritems() %}
{{zone}}:
{% for reservation in reservations %}
{{reservation}}
{% endfor %}
{% endfor %}
在您自己的尝试中,zone
仅设置到每个键,在您的情况下是单字符字符串('1'
、'2'
和 '3'
).遍历单个字符的字符串或使用 zone[0]
索引该字符串只会导致显示该字符。