2015-06-05 99 views
-2

我想創建一個方法,查看mainDict中的每個值,返回包含filterDict中列出的所有鍵值對的所有值的數組。如何通過其他字典過濾python字典?

def where(mainDicts, filterDict): 
    pass 

mydicts = [{'title': "title 1", 'author': "author 1", 'year': 1611}, 
      {'title': "title 2", 'author': "author 2", 'year': 1615}, 
      {'title': "title 3", 'author': "author 1", 'year': 1611}] 

filterDict = {'year': 1611, 'author': "author 1"} 

where(mydicts, filterDict) 

我想回到這個:

[{'title': "title 1", 'author': "author 1", 'year': 1611}, 
{'title': "title 3", 'author': "author 1", 'year': 1611}] 
+0

你能在實施後你的企圖? –

+2

SO不是代碼寫入服務。請嘗試併發布您可能面臨的任何錯誤。 – sgp

回答

2

假設你運行Python 3:

def where(mainDicts, filterDict): 
    return [x for x in mainDicts if not filterDict.items() - x.items()] 

引用文檔:

鍵看法載樣,因爲他們的作品是獨一無二的。 如果所有的值都是可散列的,那麼(key, value)對是唯一的,而 可散列,那麼items視圖也是類似的。對於集合式視圖,爲抽象基類collections.abc.Set定義的所有操作都可用(例如,==,<^)。

有關更多信息,請參閱Dictionary view objects。 如果你需要這個在Python 2中工作,只需將items()替換爲viewitems()即可。

例子:

In [8]: where(mydicts, {'year': 1611, 'author': "author 1"}) 
Out[8]: 
[{'author': 'author 1', 'year': 1611, 'title': 'title 1'}, 
{'author': 'author 1', 'year': 1611, 'title': 'title 3'}] 

注意上面的,如果你的價值不是可哈希(見Glossary)將無法正常工作,但下面將

def where(dicts, filt): 
    return [x for x in dicts if all(k in x and x[k] == v for k, v in filt.items())] 
+0

不錯!但恕我直言,如果filterDict.items()<= x。items()'更容易理解。 –

1

只需用一個列表理解,對於每個項目d檢查filterDict中的所有鍵k是否在該項中,如果是,則v的值是否相同。

def where(mainDict, filterDict): 
    return [d for d in mainDict if all(k in d and d[k] == v 
             for k, v in filterDict.items())] 

這也與Python的作品2.舉例:

>>> where(mydict, {'year': 1611, 'author': "author 1"}) 
[{'author': 'author 1', 'year': 1611, 'title': 'title 1'}, 
{'author': 'author 1', 'year': 1611, 'title': 'title 3'}] 
0

您也可以使用dict.viewitems讓你的字典和詞典過濾器,然後在路口與平等之間的交集更Python的方式過濾器可以返回它:

>>> filt={'author': 'author 1', 'year': 1611} 
>>> [d for d in mydict if dict(filt.viewitems()&d.viewitems())==filt] 
[{'author': 'author 1', 'year': 1611, 'title': 'title 1'}, {'author': 'author 1', 'year': 1611, 'title': 'title 3'}] 

閱讀更多關於字典視圖對象https://docs.python.org/2/library/stdtypes.html#dictionary-view-objects

0

這是另一種方式來做到這一點,也許它對你來說更具可讀性。這是蟒蛇2和3兼容,非常簡單。

def where(list_of_dicts, filterDict): 

    result = [] # to be returned, a list of dicts 

    for d in list_of_dicts: 
     n = 0 # count how many key/value pairs matches with filterDict 
     for key in filterDict: 
      try: # in case key is missing 
       if d[key] == filterDict[key]: 
        n += 1 
      except: 
       pass # change with a proper error message 
     if n == len(filterDict): # if True then all the key/value pairs in filterDict are in d 
      result.append(d) 

    return result 




mydicts = [{'title': "title 1", 'author': "author 1", 'year': 1611}, 
      {'title': "title 2", 'author': "author 2", 'year': 1615}, 
      {'title': "title 3", 'author': "author 1", 'year': 1611}] 

filterDict = {'year': 1611, 'author': "author 1"} 


a = where(mydicts, filterDict) 
print(a) 

它打印出:

[{'author': 'author 1', 'year': 1611, 'title': 'title 1'}, {'author': 'author 1', 'year': 1611, 'title': 'title 3'}] 
+0

如果密鑰不在字典中,'d [key]'將失敗(引發異常)。 –

+0

可以通過嘗試/除了塊 –

+0

完成:)現在應該沒問題。感謝您的通知。 –