[英]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.