[英]Check if a Dictionary is a Subset of another Dictionary with Key Value pairs
I have two Dictionaries resources, and available_resources:我有两个字典资源和 available_resources:
resources = {'B': 1, 's': 2, 't': 3, 'e': 3, '!': 1, 'h': 1, 'i': 1, ' ': 3, 'o': 1, 'g': 1, 'E': 1, 'A': 1, 'x': 2, 'p': 1, 'l': 1, 'r': 1}
available_resources = {'A': 1, 'l': 1, 'g': 1, 'o': 1, 'E': 1, 'x': 1, 'p': 1, 'e': 3, 'r': 1, 't': 3, ' ': 3, 'i': 1, 's': 2, 'h': 1, 'B': 1, '!': 1}
I want to check if resources is a subset of available_resources (if each element contained in the dictionary is <= the corresponding value entry in the resources dictionary)我想检查资源是否是 available_resources 的子集(如果字典中包含的每个元素都是 <= 资源字典中的相应值条目)
I've tried:我试过了:
if all(available_resources.get(key, None) == val for key, val
in resources.items()):
return True
It is returning false, is there another way I can get it to work?它返回false,还有其他方法可以让它工作吗?
Could it be a simple sign error?这可能是一个简单的符号错误吗? From "==" val to "<=" val?
从 "==" val 到 "<=" val? I got true from the below.
我从下面得到了真实。
if all(available_resources.get(key, None) <= val for key, val
in resources.items()):
return True
If all the values are integers, one approach is to use collections.Counter :如果所有值都是整数,一种方法是使用collections.Counter :
from collections import Counter
resources = {'B': 1, 's': 2, 't': 3, 'e': 3, '!': 1, 'h': 1, 'i': 1, ' ': 3, 'o': 1, 'g': 1, 'E': 1, 'A': 1, 'x': 2, 'p': 1, 'l': 1, 'r': 1}
available_resources = {'A': 1, 'l': 1, 'g': 1, 'o': 1, 'E': 1, 'x': 1, 'p': 1, 'e': 3, 'r': 1, 't': 3, ' ': 3, 'i': 1, 's': 2, 'h': 1, 'B': 1, '!': 1}
res = bool(Counter(resources) - Counter(available_resources))
print(res)
Output输出
True
You can use the <=
operator from sets.您可以使用集合中的
<=
运算符。 This operator determines whether one set is a subset of the other.此运算符确定一个集合是否是另一个集合的子集。
As follows:如下:
>>> resources.items() <= available_resources.items()
False
This returns False
as there is a difference between the element x
in the different dict.这将返回
False
因为不同字典中的元素x
之间存在差异。 You can see this difference using the set operator ^
with will return you the symmetric difference between the dict
:您可以使用集合运算符
^
看到这种差异,它将返回dict
之间的对称差异:
>>> resources.items() ^ available_resources.items()
{('x', 1), ('x', 2)}
You need to use <=
instead of ==
您需要使用
<=
而不是==
>>> all(available_resources.get(k, -1)<=v for k,v in resources.items())
True
Also, above method may fail if resources
contains some key that doesn't exist in available_resources
, and you can additionally check if the keys in resources are subset of the keys in available_resources for this condition此外,如果
resources
包含一些在available_resources
中不存在的键,则上述方法可能会失败,并且您可以额外检查资源中的键是否是这种情况下 available_resources 中的键的子集
>>> all(available_resources.get(k, -1)<=v for k,v in resources.items()) and\
set(resources).issubset(available_resources)
True
I have tested the answers in this stackoverflow question: click here我已经测试了这个 stackoverflow 问题中的答案: 点击这里
And i think it's works for you!我认为它对你有用!
all(item in available_resources.items() for item in resources.items())
# - or - #
available_resources.items() <= resources.items()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.