迭代字典

Iteration over dictionary

我设置了一个 Flask 应用程序,试图从 classic Drinks API 中获取数据。

这是此 API

给出的输出的简短示例
{
  "drinks": [
    {
      "idDrink": "13196",
      "strDrink": "Long vodka",
      "strCategory": "Ordinary Drink",
      "strGlass": "Highball glass",
      "strInstructions": "Shake a tall glass with ice cubes and Angostura, coating the inside of the glass. Por the vodka onto this, add 1 slice of lime and squeeze juice out of remainder, mix with tonic, stir and voila you have a Long Vodka",
      "strIngredient1": "Vodka",
      "strIngredient2": "Lime",
      "strIngredient3": "Angostura bitters",
      "strIngredient4": "Tonic water",
      "strIngredient5": "Ice",
      "strIngredient6": null,
      "strIngredient7": null,
      "strIngredient8": null,
      "strIngredient9": null,
      "strIngredient10": null,
      "strIngredient11": null,
      "strIngredient12": null,
      "strIngredient13": null,
      "strIngredient14": null,
      "strIngredient15": null
    },
    {
      "idDrink": "16967",
      "strDrink": "Vodka Fizz",
      "strCategory": "Other/Unknown",
      "strGlass": "White wine glass",
      "strInstructions": "Blend all ingredients, save nutmeg. Pour into large white wine glass and sprinkle nutmeg on top.",
      "strIngredient1": "Vodka",
      "strIngredient2": "Half-and-half",
      "strIngredient3": "Limeade",
      "strIngredient4": "Ice",
      "strIngredient5": "Nutmeg",
      "strIngredient6": null,
      "strIngredient7": null,
      "strIngredient8": null,
      "strIngredient9": null,
      "strIngredient10": null,
      "strIngredient11": null,
      "strIngredient12": null,
      "strIngredient13": null,
      "strIngredient14": null,
      "strIngredient15": null
    }
  ]
}

如何迭代具有相同名称但以其他名称结尾的值?
例如,他们有:strIngredient1-15.

不确定,但也许正则表达式更好或 match/endswith 然后使用 {{ variable }} 来呈现它。

我知道可以通过多种方式完成,但我不知道如何继续,我只需要朝着正确的方向推动。

我试图遍历它,但出现错误

NoneType is not iterable

在线

{% if 'strIngredient' in inner_dict[ingr] == null %}

我的完整代码是

<tbody>
    {% for outer_dict in data_output %}
        {% for inner_dict in data_output[outer_dict] %}
    <tr>
        <td>{{ inner_dict['idDrink'] }}</td>
        <td>{{ inner_dict['strDrink'] }}</td>
        <td>{{ inner_dict['strCategory'] }}</td>
        <td>{{ inner_dict['strGlass'] }}</td>
        <td>{{ inner_dict['strInstructions'] }}</td>
            {% for ingr in inner_dict %}
                {% if 'strIngredient' in inner_dict[ingr] == null %}
            <td>{{ inner_dict[ingr] }}</td>
    </tr>
                {% endif %}
            {% endfor %}
        {% endfor %}
    {% endfor %}
</tbody>

因为你必须将字典的属性与 strIngredient 进行比较,所以想法应该是使用 for construction to loop over a dictionary:

As variables in templates retain their object properties, it is possible to iterate over containers like dict:

<dl>
{% for key, value in my_dict.items() %}
   <dt>{{ key|e }}</dt>
   <dd>{{ value|e }}</dd>
{% endfor %}
</dl>

有了这个,您就拥有了字典的键和值,并且可以更容易地将您的字符串与 for 本身给定的键进行比较。

注意:

  • Jinja 中还有一个 for ... if ... 结构可以简化您的实际用例
  • 您可以在特定用例中使用 Python 字符串的 startswith 方法:drink_attribute.startswith('strIngredient')
  • 您还必须检查配料是否不是 None,因为您在输入 JSON
  • 中有一些 null 配料值
  • 在您的代码中提供有意义的名称总是有用的(例如 drinks — 复数,因为它是一个饮料列表 — 而不是 data_outputdrink 而不是 outer_dict, ...).

所有这些共同构成了成分循环:

{%- for drink_attribute, ingredient in drink.items() 
    if drink_attribute.startswith('strIngredient') and ingredient 
%}
  <td>{{ ingredient }}</td>
{%- endfor %}

这是创建一个包含所有饮料的 table 的循环:

<table>
  <tbodby>
    {% for drink in drinks %}
    <tr>
      <td>{{ drink.idDrink }}</td>
      <td>{{ drink.strDrink }}</td>
      <td>{{ drink.strCategory }}</td>
      <td>{{ drink.strGlass }}</td>
      <td>{{ drink.strInstructions }}</td>
      
      {%- for drink_attribute, ingredient in drink.items() 
            if drink_attribute.startswith('strIngredient') and ingredient 
      %}
        <td>{{ ingredient }}</td>
      {%- endfor %}
    </tr>
    {% endfor %}
  <tbody>
<table>

这将呈现(为便于阅读而添加了一些间距和样式):

table {
  border-collapse: collapse;
}
td {
  border: 1px solid;
}
<table>
  <tbodby>
    
    <tr>
      <td>13196</td>
      <td>Long vodka</td>
      <td>Ordinary Drink</td>
      <td>Highball glass</td>
      <td>
          Shake a tall glass with ice cubes and Angostura, 
          coating the inside of the glass. 
          Por the vodka onto this, 
          add 1 slice of lime and squeeze juice out of remainder,
          mix with tonic, 
          stir and voila you have a Long Vodka
      </td>
        <td>Angostura bitters</td>
        <td>Vodka</td>
        <td>Tonic water</td>
        <td>Ice</td>
        <td>Lime</td>
    </tr>
    
    <tr>
      <td>16967</td>
      <td>Vodka Fizz</td>
      <td>Other/Unknown</td>
      <td>White wine glass</td>
      <td>
          Blend all ingredients, save nutmeg. 
          Pour into large white wine glass 
          and sprinkle nutmeg on top.
      </td>
        <td>Limeade</td>
        <td>Vodka</td>
        <td>Ice</td>
        <td>Nutmeg</td>
        <td>Half-and-half</td>
    </tr>
    
  <tbody>
<table>