簡體   English   中英

檢查dict值是否作為另一個dict中的鍵存在

[英]Check if dict values exist as keys in another dict

我有兩個字典:

concave = {6: [2, 3, 4, 5], 2: [6], 3: [6], 4: [6], 5: [6]}
convex = {1: [2, 3, 4, 5], 2: [1, 3, 5], 3: [1, 2, 4], 4: [1, 3, 5], 5: [1, 2, 4], 6: [7, 8, 9, 10], 7: [6, 8, 10, 11], 8: [6, 7, 9, 11], 9: [6, 8, 10, 11], 10: [6, 7, 9, 11], 11: [7, 8, 9, 10]}

而且我在凸面字典中返回了具有最大長度值的鍵:

max_lens = [1, 6, 7, 8, 9, 10, 11]

對於max_lens每個數字,我要檢查它是否不作為concave的鍵存在, 並且convex 值是否作為concave鍵存在。

因此,在此示例中,“ 1”將滿足此條件,因為它不包含在concave作為鍵,但是其convex中的值為(即2、3、4和5)。

我試圖弄清楚如何使用for循環/ if語句:

for i in enumerate(max_lens):
    if i not in concave:
        for k,v in convex.items():
            for j in v:

那是我在完全困惑之前得到的。 除了使用多個for循環和if語句之外,還必須有一種更簡單的方法來執行此操作。

我有點蟒蛇,很抱歉,如果遇到這種混亂!

我了解(出於記錄的考慮,我更喜歡顯式concave.keys()

result_dict = {}
for convex_key in max_lens:
    result_dict[convex_key] = convex_key not in concave.keys() \
                              and all(convex_val in concave.keys() 
                                      for convex_val in convex[convex_key])

編輯(請參閱評論)

for convex_key in max_lens:
    if convex_key not in concave.keys() and \
           all(convex_val in concave.keys() for convex_val in convex[convex_key]):
        top_face = convex_key
        break

逐步解決此問題總是有幫助的:

  • 循環遍歷max_lens每個長度l
  • 檢查l是否不存在於concave而存在於convex 這里需要將這兩個條件結合起來。 如果任何一個失敗,請不要繼續。
  • 如果接受了以上兩個條件,請檢查是否所有來自convex[l]的值都存在於concave
  • 如果代碼沒有問題到達此處,則說明所有條件都已滿足。

演示:

concave = {6: [2, 3, 4, 5], 2: [6], 3: [6], 4: [6], 5: [6]}
convex = {1: [2, 3, 4, 5], 2: [1, 3, 5], 3: [1, 2, 4], 4: [1, 3, 5], 5: [1, 2, 4], 6: [7, 8, 9, 10], 7: [6, 8, 10, 11], 8: [6, 7, 9, 11], 9: [6, 8, 10, 11], 10: [6, 7, 9, 11], 11: [7, 8, 9, 10]}

max_lens = [1, 6, 7, 8, 9, 10, 11]

for l in max_lens:
    if l not in concave and l in convex and all(v in concave for v in convex[l]):
        print(l)

輸出:

1

使用簡單的forloop。

concave = {6: [2, 3, 4, 5], 2: [6], 3: [6], 4: [6], 5: [6]}
convex = {1: [2, 3, 4, 5], 2: [1, 3, 5], 3: [1, 2, 4], 4: [1, 3, 5], 5: [1, 2, 4], 6: [7, 8, 9, 10], 7: [6, 8, 10, 11], 8: [6, 7, 9, 11], 9: [6, 8, 10, 11], 10: [6, 7, 9, 11], 11: [7, 8, 9, 10]}
max_lens = [1, 6, 7, 8, 9, 10, 11]

for i in max_lens:
    if (i not in concave):   #Check if not in key.
        if convex[i] in concave.values():   #Check Value.
                print i

輸出

1

您可以通過理解來做到這一點:

[i for i in max_lens if i not in concave and convex[i] in concave.values()]

如果您不容易理解問題,將其分為幾個較小的問題通常是個好方法:

  1. 編寫一個檢查值是否不是字典鍵的函數:

def is_no_key_in(v, _dict): return key not in _dict

  1. 由於這太簡單了,請返回不在dict中的鍵列表:

def no_key_values(_list, _dict): return [ v for v in _list if is_no_key_in(v, _dict) ]

  1. 現在,您只有符合第一個條件的值,您可以專注於第二個條件。 由於您希望列表的每個值都在鍵列表中,因此可以開始創建類似聯合的函數:

def union(a_lst, b_lst): return [ a for a in a_lst if a in b_lst]

  1. 為了使其更符合您的需求,您可以將其更改為檢查差異的函數:

def is_subset(a_lst, b_lst): return len([a for a in a_lst if a not in b_lst]) == 0

  1. 現在,將功能組合在一起:

def satisfies_conditions(max_lens): for lens in no_key_values(max_lens, concave): if is_subset(convex[lens], concave.keys()) yield lens result = [ lens for lens in satisfies_conditions(max_lens) ]

result現在包含滿足您條件的所有鏡頭,如果您要更改條件,則可以輕松實現。 如果您的代碼有效,則可以繼續對其進行重構。 例如,您可能不需要is_no_key_in因為它是一個非常簡單的函數。 然后繼續並將其內聯到no_key_values

def no_key_values(_list, _dict): return [ v for v in _list if v not in _dict ]如果在重構(甚至編寫代碼)之前編寫了一些測試,則可以確保重構不會引入錯誤。 然后逐步簡化代碼。 也許您最終會得到一個像此處其他答案中所建議的那樣簡單的解決方案。

(我希望這也可以幫助您解決諸如此類的未來問題:-))

暫無
暫無

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

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