[英]Check if all the pairs in a dict also exist in another dict, possibly with equal or lower corresponding values
[英]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()]
如果您不容易理解問題,將其分為幾個較小的問題通常是個好方法:
def is_no_key_in(v, _dict): return key not in _dict
def no_key_values(_list, _dict): return [ v for v in _list if is_no_key_in(v, _dict) ]
def union(a_lst, b_lst): return [ a for a in a_lst if a in b_lst]
def is_subset(a_lst, b_lst): return len([a for a in a_lst if a not in b_lst]) == 0
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.