小编dja*_*eur的帖子

如何在django模板中迭代嵌套字典

我不确定迭代嵌套字典的最有效方法是为每个日期打印每个水果的总值和良好值的矩阵.以下面的两个列表和字典为例:

fruits = ['apples','oranges','bananas']
harvest_dates = ['2011-07-23','2011-07-22','2011-07-21']

harvest_data = {
  'apples': {
    '2011-07-23': {
      'total': 100,
      'good': 80},
     '2011-07-22': {
       'total': 97,
       'good': 92},
     '2011-07-21': {
       'total': 90,
       'good': 85}
  },
  'oranges': {
    '2011-07-23': {
      'total': 86,
      'good': 82},
    '2011-07-22': {
      'total': 90, 
      'good': 75},
    '2011-07-21': {
      'total': 92,
      'good': 92}
  },
  'bananas': {
    '2011-07-23': {
      'total': 10,
      'good': 9},
    '2011-07-22': {
      'total': 12, 
      'good': 11},
    '2011-07-21': {
      'total': 9,
      'good': 9}
  }
}
Run Code Online (Sandbox Code Playgroud)

我可以在python中轻松完成:

for fruit in fruits:
  for …
Run Code Online (Sandbox Code Playgroud)

django django-templates

29
推荐指数
2
解决办法
2万
查看次数

标签 统计

django ×1

django-templates ×1