Home > Software engineering >  Query to a list of dictionaries (python)
Query to a list of dictionaries (python)

Time:11-13

I have a list of dictionaries:

friends = [
{'name': 'Sam', 'gender': 'male', 'sport': 'Basketball'},
{'name': 'Emily', 'gender': 'female', 'sport': 'volleyball'},
]

I need to create functions query, select, and field_filter to work with similar lists. These functions have to provide a possibility to select necessary columns and make filtering by these columns.

Example:

result = query(friends, 
select('name', 'gender', 'sport'), 
field_filter('sport', *('Basketball', 'volleyball')), 
field_filter('gender', *('male',)))`

[{'gender': 'male', 'name': 'Sam', 'sport': 'Basketball'}]

I need to use predefined code:

from typing import Dict, Any, Callable, Iterable
DataType = Iterable[Dict[str, Any]]
ModifierFunc = Callable[[DataType], DataType]
def query(data: DataType, selector: ModifierFunc,
      *filters: ModifierFunc) -> DataType:
"""Query data with column selection and filters
:param data: List of dictionaries with columns and values
:param selector: result of `select` function call
:param filters: Any number of results of `field_filter` function calls
:return: Filtered data"""
pass
def select(*columns: str) -> ModifierFunc:
"""Return function that selects only specific columns from dataset"""
pass
def field_filter(column: str, *values: Any) -> ModifierFunc:
"""Return function that filters specific column to be one of `values`"""
pass

CodePudding user response:

Although the question implies that select and field_filter might want to be classes, I don't think that's necessary here; I'd just make them return regular old tuples:

select = field_filter = lambda *args: args

and then query is just a list and dict comprehension where you iterate over the list of dicts and return the selected fields from the dicts that match the field_filters:

def query(data, keys, *filters):
    return [
        {k: d[k] for k in keys}
        for d in data
        if all(d[k] in v for k, *v in filters)
    ]
friends = [
    {'name': 'Sam', 'gender': 'male', 'sport': 'Basketball'},
    {'name': 'Emily', 'gender': 'female', 'sport': 'volleyball'},
]
result = query(
    friends,
    select('name', 'gender', 'sport'),
    field_filter('sport', *('Basketball', 'volleyball')),
    field_filter('gender', *('male',))
)
print(result)  # [{'name': 'Sam', 'gender': 'male', 'sport': 'Basketball'}]
  • Related