擅长:python、mysql、java
<p>这应该起作用:</p>
<pre class="lang-py prettyprint-override"><code>data = {
"shopName": "Shop",
"promotions": [
{
"productName": "Cookies",
"oldPrice": 11.99,
"newPrice": 7.99,
"discount": 33
},
{
"productName": "Butter",
"oldPrice": 27.15,
"newPrice": 21.99,
"discount": 19
},
{
"productName": "Milk",
"oldPrice": 30.45,
"newPrice": 21.99,
"discount": 27
}
]
}
MIN_PRICE = 20
filtered_products = [p for p in data['promotions'] if p['discount'] >= MIN_PRICE]
print(filtered_products)
</code></pre>
<p>这张照片是:</p>
<pre><code>[
{
"productName": "Cookies",
"oldPrice": 11.99,
"newPrice": 7.99,
"discount": 33
},
{
"productName": "Milk",
"oldPrice": 30.45,
"newPrice": 21.99,
"discount": 27
}
]
</code></pre>
<p>另一种方法是使用<code>filter</code>函数:</p>
<pre class="lang-py prettyprint-override"><code>filtered_products = list(filter(lambda p: p['discount'] > MIN_PRICE, data['promotions']))
</code></pre>