擅长:python、mysql、java
<pre><code>data = [
{
"dimensions" : {
"width": 50,
"height": 75,
"color": 'red',
},
"group": "starter",
},
# We want this one
{
"dimensions": {
"width": 150,
"height": 25,
"color": 'blue',
},
"group": "starter",
},
{
"dimensions": {
"width": 10,
"height": 5,
"color": 'yellow',
},
"group": "primary",
}
]
def find_match(data, height=0, width=0):
"""Return match based on height & width"""
for item in data:
if (item["dimensions"]["height"] == height) \
and (item["dimensions"]["width"] == width):
return item
return None
print('Found: {}'.format(find_match(data, 25, 150))) # Match found
print('Found: {}'.format(find_match(data, 100, 100))) # No match found
</code></pre>