python按字典过滤字典列表,包含几个键值对作为条件

int*_*ety 5 python dictionary list filter matching

我的示例数据:

list_of_dict =[{'cena': 23, 'nazwa': 'item1', 'param': 'pampam'},
               {'cena': 26, 'nazwa': 'item2', 'param': 'iko'   },
               {'cena': 26, 'nazwa': 'item2a','param': 'ik2'   },
               {'cena': 26, 'nazwa': 'item2b','param': 'ik2'   },
               {'cena': 17, 'nazwa': 'item3', 'param': 'etr'   },
               {'cena': 17, 'nazwa': 'item4', 'param': 'asdf'  }]

conditions =   {'cena': 26, 'param': 'ik2' }
Run Code Online (Sandbox Code Playgroud)

我试过:

if conditions in list_of_dict:
    do_something()
Run Code Online (Sandbox Code Playgroud)

它有效,但只有当整个条件字典(每个键)与字典列表中的一个匹配时,我的意思是:

In [1]: exampleSet =      [{  'type' : 'type1', 'k' : 'kval'},
   ...:                    {  'type' : 'type2', 'k' : 'kv2' },
   ...:                    {  'type' : 'type2', 'k' : 'k3'  },
   ...:                    {  'type' : 'type3', 'k' : 'k3'  }]
   ...: 
   ...: conditions =       {  'type' : 'type1', 'k' : 'kval' }
   ...: 
   ...: 
   ...: conditions in exampleSet
   ...: 
Out[1]: True
In [2]: conditions =       {  'type' : 'type1' }

In [3]: conditions in exampleSet
Out[3]: False
Run Code Online (Sandbox Code Playgroud)

当我尝试将字典与指定的键值对匹配时,(无论值/未指定值是否存在)所以

In [4]: exampleSet =      [{  'type' : 'type1', 'k' : 'kval'},
   ...:                    {  'type' : 'type2', 'k' : 'kv2' },
   ...:                    {  'type' : 'type2', 'k' : 'k3'  },
   ...:                    {  'type' : 'type3', 'k' : 'k3'  }]
   ...: 
   ...: conditions =       {  'type' : 'type2' }
   ...:
   ...: my_wanted_match( exampleSet, conditions )
Run Code Online (Sandbox Code Playgroud)

必须返回:

                     [{  'type' : 'type2', 'k' : 'kv2' },
                      {  'type' : 'type2', 'k' : 'k3'  }]
Run Code Online (Sandbox Code Playgroud)

其结果。

任何人都可以给一些关于如何实现这一目标的提示吗?

haa*_*vee 5

这是filter()您想要的 - 您想根据某些条件过滤您的字典列表;仅返回符合所有条件的条目。

>>> list_of_dict =[{'cena': 23, 'nazwa': 'item1', 'param': 'pampam'},
...                {'cena': 26, 'nazwa': 'item2', 'param': 'iko'   },
...                {'cena': 26, 'nazwa': 'item2a','param': 'ik2'   },
...                {'cena': 26, 'nazwa': 'item2b','param': 'ik2'   },
...                {'cena': 17, 'nazwa': 'item3', 'param': 'etr'   },
...                {'cena': 17, 'nazwa': 'item4', 'param': 'asdf'  }]
Run Code Online (Sandbox Code Playgroud)

设置条件:

>>> conditions = {'param':'iko'}
Run Code Online (Sandbox Code Playgroud)

并做一个单行过滤器:

>>> filter(lambda item: all((item[k]==v for (k,v) in conditions.iteritems())), list_of_dict)
[{'cena': 26, 'param': 'iko', 'nazwa': 'item2'}]
Run Code Online (Sandbox Code Playgroud)


se7*_*7en 0

def my_wanted_match(example_set, conditions):
    conditions = conditions.items()
    match = []
    for e in example_set:
        if all([c in set(e.items()) for c in conditions]):
            match.append(e)
    return match
Run Code Online (Sandbox Code Playgroud)

或者简单地:

match = [e for e in example_set
         if all([c in set(e.items()) for c in set(conditions.items())])]
Run Code Online (Sandbox Code Playgroud)