簡體   English   中英

python 逐個過濾字典列表,包含幾個鍵值對作為條件

[英]python filtering list of dict by dict containing several key-value pairs as conditions

我的示例數據:

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' }

我試過了:

if conditions in list_of_dict:
    do_something()

它有效,但只有當整個條件 dict (每個鍵)與 dict 列表中的一個匹配時,我的意思是:

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

當我試圖將字典與指定的鍵值對匹配時,(不管值/存在未指定的值)所以

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 )

必須返回:

                     [{  'type' : 'type2', 'k' : 'kv2' },
                      {  'type' : 'type2', 'k' : 'k3'  }]

因此。

誰能給我一些關於如何實現這一目標的提示?

它是您想要的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'  }]

設定條件:

>>> conditions = {'param':'iko'}

並做一個單行過濾器:

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

這將遍歷list_of_dict每個字典,並將具有匹配鍵值對的字典返回到每個條件。

matches = [one_dict for one_dict in list_of_dict if
           all(key in one_dict and conditions[key] == one_dict[key] 
               for key in conditions.keys())]
>>> matches
[{'cena': 26, 'nazwa': 'item2a', 'param': 'ik2'},
 {'cena': 26, 'nazwa': 'item2b', 'param': 'ik2'}]

使用清單理解:

>>> 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' }
>>> [d for d in list_of_dict if all((k in d and d[k] == v) for k, v in conditions.items())]
[{'cena': 26, 'param': 'ik2', 'nazwa': 'item2a'}, {'cena': 26, 'param': 'ik2', 'nazwa': 'item2b'}]
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

或者簡單地:

match = [e for e in example_set
         if all([c in set(e.items()) for c in set(conditions.items())])]

set在這里派上用場*

has_match = any(set(condition.items()) <= set(d.items()) for d in listOfDict)

或第二部分:

the_matches = [
    d
    for d in listOfDict
    if set(condition.items()) <= set(d.items())
]

<=是應用於集合的子集運算符:

issubset(other)
set <= other

測試集合中的每個元素是否都在另一個元素中。

* 只要您可以確保您的值是可哈希的,即不是列表或字典

    enter code here
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'  }]

>Answers
 --------

res=list(filter(lambda list_of_dict:list_of_dict['cena']== 26 and list_of_dict['param']== 'ik2',list_of_dict))

for i in range(0,len(res)):
    if 'item2b' in res[i].values() :
        print(res[i])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM