简体   繁体   中英

Extract matching elements from array

I have the following python3 array..

[
    {
        "dimensions" : {
            "width"    : 50,
            "height"   : 75,
            "color"  : 'red',
        },
        "group": "starter",
    },
    {
        "dimensions" : {
            "width"    : 150,
            "height"   : 25,
            "color"  : 'blue',
        },
        "group": "starter",
    },
    {
        "dimensions" : {
            "width"    : 10,
            "height"   : 5,
            "color"  : 'yellow',
        },
        "group": "primary",
    },
]

I have a known width and height so I am trying to create a new array that contains the item that matches these values.

So my known width and height is 150*25 so I want my new array to look like this...

[
    {
        "dimensions" : {
            "width"    : 150,
            "height"   : 25,
            "color"  : 'blue',
        },
        "group": "starter",
    },
]

I haven't been able to find an example to follow, does anybody have one?

A list comprehension will work. Assuming you have the data in a list called data :

filtered_data = [item for item in data if item['dimensions']['width'] == 150
                                       and item['dimensions']['height'] = 25]
data = [
    {
        "dimensions" : {
            "width"    : 50,
            "height"   : 75,
            "color"  : 'red',
        },
        "group": "starter",
    },
    {
        "dimensions" : {
            "width"    : 150,
            "height"   : 25,
            "color"  : 'blue',
        },
        "group": "starter",
    },
    {
        "dimensions" : {
            "width"    : 10,
            "height"   : 5,
            "color"  : 'yellow',
        },
        "group": "primary",
    },
]


def search(x,y):
    for item in data:
        if x in item['dimensions'].values() and y in item['dimensions'].values():
            return item

x = 150
y = 25
print (search(x,y))

output:

{'dimensions': {'width': 150, 'height': 25, 'color': 'blue'}, 'group': 'starter'}
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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM