Python 找出一本字典';s元素比其他字典中的元素大

Python 找出一本字典';s元素比其他字典中的元素大,python,Python,我希望有一个循环(例如While循环),用于检查字典中的所有元素是否都比其他字典中的元素大。 这些是需要检查的需求字典和两个示例字典: requirements = {"happiness":9, "money": 100} person1 = {"happiness": 15, "money": 3200} person2 = {"happiness": 4, "money": 5000} While循环将如下所示: while person >= requirements:

我希望有一个循环(例如While循环),用于检查字典中的所有元素是否都比其他字典中的元素大。 这些是需要检查的需求字典和两个示例字典:

requirements = {"happiness":9, "money": 100}

person1 = {"happiness": 15, "money": 3200}
person2 = {"happiness": 4, "money": 5000}
While循环将如下所示:

while person >= requirements:
    p = Counter(person)
    p.subtract(requirements)
    person = dict(p)
all(value > requirements[key] for key, value in person1.items())
当检查人员是否满足要求时,我希望只有在满足所有要求的情况下才是真的


注意:while循环中从人员中减去需求的部分可以忽略,它与问题无关,但它是我正在使用的代码,不确定while循环是用于什么,但我认为在这种情况下,一个简单的while循环就足够了

from operator import gt

people = [person1, person2]
for person in people:
    thresholds = [gt(person.get(k), requirements.get(k, 0)) for k in person.keys()]
    if all(thresholds):
        print(f'Person --> {person} meets requirements')

Person --> {'happiness': 15, 'money': 3200} meets requirements

注意:f-strings只在>=Python3.6上工作,不确定while循环的作用是什么,但我认为在这种情况下,一个简单的
for
循环就足够了

from operator import gt

people = [person1, person2]
for person in people:
    thresholds = [gt(person.get(k), requirements.get(k, 0)) for k in person.keys()]
    if all(thresholds):
        print(f'Person --> {person} meets requirements')

Person --> {'happiness': 15, 'money': 3200} meets requirements

注意:f-strings仅适用于>=Python 3.6

您不需要Python中的显式循环来解决此类问题,请使用以下理解:

while person >= requirements:
    p = Counter(person)
    p.subtract(requirements)
    person = dict(p)
all(value > requirements[key] for key, value in person1.items())

您不需要Python中的显式循环来解决此类问题,请使用以下理解:

while person >= requirements:
    p = Counter(person)
    p.subtract(requirements)
    person = dict(p)
all(value > requirements[key] for key, value in person1.items())

希望我的问题能给你启发。我的功能
大于需求()
使用
require
中的所有键检查一个人
person
。只有当
所有的值大于
请求时,它才会返回
True

#!/usr/bin/env python3

def bigger_than_requirements(person, require):
    for key in require:
        if person[key] <= require[key]:
            return False

    return True

if __name__ == '__main__':
    all_persons = [{"happiness": 15, "money": 3200},
                   {"happiness": 4, "money": 5000}]
    requirements = {"happiness": 9, "money": 100}

    for person in all_persons:
        if bigger_than_requirements(person, requirements):
            print('Bigger: {}'.format(person))

希望我的问题能给你启发。我的功能
大于需求()
使用
require
中的所有键检查一个人
person
。只有当
所有的值大于
请求时,它才会返回
True

#!/usr/bin/env python3

def bigger_than_requirements(person, require):
    for key in require:
        if person[key] <= require[key]:
            return False

    return True

if __name__ == '__main__':
    all_persons = [{"happiness": 15, "money": 3200},
                   {"happiness": 4, "money": 5000}]
    requirements = {"happiness": 9, "money": 100}

    for person in all_persons:
        if bigger_than_requirements(person, requirements):
            print('Bigger: {}'.format(person))

如果我再多想一点的话,我可能也会想到这个问题。但是这正是我所寻找的答案。如果我再多想一点的话,我可能也会想到这个问题。但是这正是我所寻找的答案