ajoseps
ajoseps

Reputation: 2101

How do I pass in an argument to a list of lambdas?

Intent: I'm trying to return each dictionary that contains the passed in matching keywords and values within a list of dictionaries. For example, a='woot', e='1', c='duh' would return only

{'a': 'woot', 'b': 'nope', 'c': 'duh', 'd': 'rough', 'e': '1'}

This is what I have so far, but I don't know how to pass in an argument to a list of lambda expression which act as the filter for each dictionary in the list.

sample_dict = [
{'a': 'woot', 'b': 'nope', 'c': 'duh', 'd': 'rough', 'e': '1'},
{'a': 'coot', 'b': 'nope', 'c': 'ruh', 'd': 'rough', 'e': '2'},
{'a': 'doot', 'b': 'nope', 'c': 'suh', 'd': 'rough', 'e': '3'},
{'a': 'soot', 'b': 'nope', 'c': 'fuh', 'd': 'rough', 'e': '4'},
{'a': 'toot', 'b': 'nope', 'c': 'cuh', 'd': 'rough', 'e': '1'}
]


def get_matched_lines(input_dict, **param):
    filters = [lambda input_dict_elem,
                      input_key=param_key,
                      input_value=param_value:
               input_dict_elem[input_key] == input_value
               for param_key, param_value in param.items()]
    return [dict_elem for dict_elem in input_dict if(all(filters))]

print(get_matched_lines(sample_dict, a='woot', e='1', c='duh'))

Upvotes: 0

Views: 163

Answers (3)

kskim
kskim

Reputation: 1

I think you don't have to use lambda here... basic for loop is enough...

Concept is simple as the following: If all the keys and values in param belongs and equals to the input_dict, it returns the whole dictionary row which you want to return. If at least a key can't be found or a value isn't the same, return nothing.

sample_dict = [
{'a': 'woot', 'b': 'nope', 'c': 'duh', 'd': 'rough', 'e': '1'},
{'a': 'coot', 'b': 'nope', 'c': 'ruh', 'd': 'rough', 'e': '2'},
{'a': 'doot', 'b': 'nope', 'c': 'suh', 'd': 'rough', 'e': '3'},
{'a': 'soot', 'b': 'nope', 'c': 'fuh', 'd': 'rough', 'e': '4'},
{'a': 'toot', 'b': 'nope', 'c': 'cuh', 'd': 'rough', 'e': '1'}
]


def get_matched_lines(input_dict, **param):
    return [d_ for d_ in sample_dict if all([False for k in param if not k in d_ or d_[k]!=param[k]])]

print(get_matched_lines(sample_dict, a='woot', e='1', c='duh'))

More question, then let me know.

Upvotes: 0

Julien Spronck
Julien Spronck

Reputation: 15423

You can do this (in Python 2.7):

def get_matched_lines(input_dict, **param):
    return [dic for dic in input_dict if all([key in dic and dic[key] == val for key, val in param.iteritems()])]

The same code in Python 3 is

def get_matched_lines(input_dict, **param):
    return [dic for dic in input_dict if all([key in dic and dic[key] == val for key, val in param.items()])]    

Upvotes: 3

Raza
Raza

Reputation: 215

def get_matched_lines(input_dict, array):
    output=[]
    keys=[]
    values=[]
    for a in array:
        keyvalue = a.split("=")
        keys.append(keyvalue[0]) 
        values.append(keyvalue[1])   
    for dict in input_dict:
        bool=1
        for i in range(0,len(keys)):
            if keys[i] in dict.keys():
                # print values[i]
                if dict[keys[i]] != values[i]:
                    bool=0
        if bool==1:
            output.append(dict)
    return output


sample_dict = [
{'a': 'woot', 'b': 'nope', 'c': 'duh', 'd': 'rough', 'e': '1'},
{'a': 'coot', 'b': 'nope', 'c': 'ruh', 'd': 'rough', 'e': '2'},
{'a': 'doot', 'b': 'nope', 'c': 'suh', 'd': 'rough', 'e': '3'},
{'a': 'soot', 'b': 'nope', 'c': 'fuh', 'd': 'rough', 'e': '4'},
{'a': 'toot', 'b': 'nope', 'c': 'cuh', 'd': 'rough', 'e': '1'}
]
array = ["a=woot", "e=1", "c=duh"]
print get_matched_lines(sample_dict, array)

output:

[{'a': 'woot', 'c': 'duh', 'b': 'nope', 'e': '1', 'd': 'rough'}]

Upvotes: 0

Related Questions