【问题标题】:Subtracting the values of two dictionaries减去两个字典的值
【发布时间】:2016-08-23 04:11:23
【问题描述】:

我正在尝试减去 dict1 - dict2。狗的结果是否为负数都没关系,我只是对学习这个概念感兴趣:)

owner = ['Bob', 'Sarah', 'Ann']

dict1 = {'Bob': {'shepherd': [4,6,3],'collie': [23, 3, 45], 'poodle':[2,0,6]}, {'Sarah': {'shepherd': [1,2,3],'collie': [3, 31, 4], 'poodle': [21,5,6]},{'Ann': {'shepherd': [4,6,3],'collie': [23, 3, 45], 'poodle': [2,10,8]}}


 dict2 = {'Bob': {'shepherd': 10,'collie': 15,'poodle': 34},'Sarah': {'shepherd': 3,'collie': 25,'poodle': 4},'Ann': {'shepherd': 2,'collie': 1,'poodle': 0}}

我想要:

wanted =   dict1 = {'Bob': {'shepherd': [-6,-4,-7],'collie': [8, -12, 30], 'poodle':[-32,-34,-28]}, {'Sarah': {'shepherd': [-2,-1,0],'collie': [-22, 6, -21], 'poodle': [17,1,2]},{'Ann': {'shepherd': [2,4,1],'collie': [22, 2, 44], 'poodle': [2,10,8]}}

我还是字典的新手,所以这是我一直在尝试但得到 NoneType 错误的方法。我假设这是因为 dict1 的值比 dict2 多,但我找不到执行上述计算的方法。

wanted = {}

for i in owner:
    wanted.update({i: dict1.get(i) - dict2.get(i)})

【问题讨论】:

    标签: python python-2.7 dictionary


    【解决方案1】:

    您可以使用嵌套的字典推导。我稍微调整了你的字典,因为它的语法不正确(有几个额外的花括号)。不需要所有者数组,因为我们可以使用字典中的键。

    dict1 = {
        'Bob': {
            'shepherd': [4, 6, 3],
            'collie': [23, 3, 45],
            'poodle': [2, 0, 6],
        },
        'Sarah': {
            'shepherd': [1, 2, 3],
            'collie': [3, 31, 4],
            'poodle': [21, 5, 6],
        },
        'Ann': {
            'shepherd': [4, 6, 3],
            'collie': [23, 3, 45],
            'poodle': [2, 10, 8],
        }
    }
    dict2 = {
        'Bob': {'shepherd': 10, 'collie': 15, 'poodle': 34},
        'Sarah': {'shepherd': 3, 'collie': 25, 'poodle': 4},
        'Ann': {'shepherd': 2, 'collie': 1, 'poodle': 0},
    }
    wanted = {
        owner: {
            dog: [num - dict2[owner][dog] for num in num_list]
            for dog, num_list in dog_dict.iteritems()
        } for owner, dog_dict in dict1.iteritems()
    }
    print wanted
    

    输出:

    {
        'Sarah': {
            'shepherd': [-2, -1, 0],
            'collie': [-22, 6, -21],
            'poodle': [17, 1, 2]
        },
        'Bob': {
            'shepherd': [-6, -4, -7],
            'collie': [8, -12, 30],
            'poodle': [-32, -34, -28]
        },
        'Ann': {
            'shepherd': [2, 4, 1],
            'collie': [22, 2, 44],
            'poodle': [2, 10, 8]
        }
    }
    

    这是假设 dict2 具有与 dict1 相同的所有键。如果不是这种情况,您需要设置一些默认值以避免KeyError

    编辑:这里简要说明了如何为不熟悉它的人解释字典理解。如果您对列表推导更满意,字典推导非常相似,只是您创建字典而不是列表。所以,{i: str(i) for i in xrange(10)} 会给你一个{0: '0', 1: '1', ..., 9: '9'} 的字典。

    现在我们可以将其应用于解决方案。这段代码:

    wanted = {
        owner: {
            dog: [num - dict2[owner][dog] for num in num_list]
            for dog, num_list in dog_dict.iteritems()
        } for owner, dog_dict in dict1.iteritems()
    }
    

    相当于:

    wanted = {}
    for owner, dog_dict in dict1.iteritems():
        wanted[owner] = {}
        for dog, num_list in dog_dict.iteritems():
            wanted[owner][dog] = [num - dict2[owner][dog] for num in num_list]
    

    【讨论】:

    • 您能解释一下嵌套字典理解的概念吗?至少如何迭代dict1。这个问题很有趣,但答案让我很费解。
    • @Monica:我不同意答案是复杂的:这是最直接和 Pythonic 的方式。一开始可能会让您感到困惑的是您有 3 个嵌套列表/字典理解。为了理解它,请尝试从内到外将它们一一分解。
    • @Monica - 我添加了一个脚注来尝试解释嵌套字典的理解。关键是逐步通过每一层。正如 Julien 所指出的,这是一个非常 Pythonic 的结构,值得学习 :)
    • @Karin。谢谢你的脚注!它有很大帮助!我对字典理解不熟悉,也不想冒犯你。
    • @Monica - 完全没有冒犯!我很高兴你问,因为我希望脚注也可以帮助其他人在未来看到这个答案:)
    猜你喜欢
    • 2016-12-11
    • 1970-01-01
    • 2014-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多