python - 减去两个字典的值

标签 python python-2.7 dictionary

我正在尝试减去 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)})

最佳答案

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

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]

关于python - 减去两个字典的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39092224/

相关文章:

python - 如何使用 python 在函数中使用列表作为参数

c++ - std::shared_ptr 的 std::map 中的线程安全

python - 使用 Python 在列表中查找匹配的字典对

python - 用另一个数组填充 numpy 数组的一列

python - wxPython中单选按钮的显示和选择问题

python - 如何在 M1 Mac 上安装 Librosa?

python - 按给定的合并顺序合并两个或多个列表

python - 如果数据冗余,重用预建字典的有效方法是什么?深拷贝太慢

python - 如何获取一个列表并以随机顺序打印所有内容,并在 python 中打印每个内容一次?

ios - 无法将类型 '[(string, string)]' 的值分配给类型 '[string : string]'