简体   繁体   中英

Subtracting the values of two dictionaries

I am trying to subtract dict1 - dict2. It does not matter if the result is a negative number for the dogs, I am just interested in learning the concept :)

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}}

I want:

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]}}

I am still new to dictionaries so here is what I have been trying but get NoneType error. I am assuming it is because dict1 has more values than dict2, but I cannot find a way to perform the above computation.

wanted = {}

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

You can use a nested dictionary comprehension. I tweaked your dictionary a bit because it wasn't correct syntax (there were a few extra curly braces). There is no need for an owners array because we can just use the keys in the dictionaries.

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

Output:

{
    '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]
    }
}

This is assuming that dict2 has all of the same keys as dict1. If that's not the case, you'll need to set some default values to avoid a KeyError .

EDIT : Here's a brief explanation of how to interpret the dictionary comprehension for those not familiar with it. If you're more comfortable with list comprehensions, a dictionary comprehension is very similar, only you create a dictionary instead of a list. So, {i: str(i) for i in xrange(10)} will give you a dictionary of {0: '0', 1: '1', ..., 9: '9'} .

Now we can apply that to the solution. This code:

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()
}

is equivalent to:

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM