1

So, what I am trying to achieve here is to take 2 dictionaries, take the values of both dictionaries in the same "spot" and be able to apply any function to it. Here's an example of some pseudo code:

If f(a, b) returns a + b
d1 = {1:30, 2:20, 3:30, 5:80}
d2 = {1:40, 2:50, 3:60, 4:70, 6:90}
then function(d1, d2) returns ({1: 70, 2: 70, 3: 90}, {4: 70, 5: 80, 6: 90})
If f(a, b) returns a > b
d1 = {1:30, 2:20, 3:30}
d2 = {1:40, 2:50, 3:60}
then function(d1, d2) returns ({1: False, 2: False, 3: False}, {})
Alex
  • 11
  • 1
  • 1
    What exactly is your question? Please check the [FAQ](http://stackoverflow.com/tour) and [How to Ask](http://stackoverflow.com/help/how-to-ask). – Rory Daulton Oct 02 '16 at 16:25
  • Have a look at this post http://stackoverflow.com/questions/10461531/merge-and-sum-of-two-dictionaries – AlvaroP Oct 02 '16 at 16:25
  • I'm not sure how to set up a function that will return the values after comparing the 2 dictionaries. Will you have to use a for loop and after use a return function? And I apologize for not asking the question, I am new to this. – Alex Oct 02 '16 at 16:26
  • As a hint, you want the set [intersection](https://docs.python.org/3/library/stdtypes.html#set.intersection) of keys to apply the function over and the [symmetric difference](https://docs.python.org/3/library/stdtypes.html#set.symmetric_difference) for the leftovers. – Ilja Everilä Oct 02 '16 at 16:30
  • Man... I am really sorry about this. A and B is basically the 2 dictionaries in a shorter term. So if f(a, b) returns a + b, then it means take a addition function and apply it to both dictionaries. – Alex Oct 02 '16 at 16:34
  • what do you mean by spot exactly ? not index right ? dictionaries are hash tables so there isn't really a particular order . you mean the values within as a conditional being the spot? – O.rka Oct 02 '16 at 20:42

2 Answers2

0

Although there maybe a more efficient way to achieve what you want, i used the information here to create the following functions:

def f1(a,b):
    return a+b

def f2(a,b):
    return a>b

def function2(d1,d2):
    out1 = {}
    out2 = {}
    #use the set operations to take the common keys
    commons = set(set(d1) & set(d2))
    for i in commons:
        out1[i] = d1[i] > d2[i]
    #all the non-common keys go to out2
    for i in d1:
        if i not in commons:
            out2[i] = d1[i]
    for i in d2:
        if i not in commons:
            out2[i] = d2[i]
    return (out1,out2)

def function1(d1,d2):
    out1 = {}
    out2 = {}
    #use the set operations to take the common keys
    commons = set(set(d1) & set(d2))
    for i in commons: out1[i] = f1(d1[i],d2[i])
    #all the non-common keys go to out2
    for i in d1:
        if i not in commons:
            out2[i] = d1[i] 
    for i in d2:
        if i not in commons:
            out2[i] = d2[i]
    return (out1,out2) 

def main():

    d1 = {1:30, 2:20, 3:30, 5:80}
    d2 = {1:40, 2:50, 3:60, 4:70, 6:90}
    d1,d2 = function1(d1,d2)
    for i in d1:print(i,d1[i])
    print('\n')
    for i in d2:print(i,d2[i])

    print('\n\n')

    d1 = {1:30, 2:20, 3:30}
    d2 = {1:40, 2:50, 3:60}
    d1,d2 = function2(d1,d2)
    for i in d1:print(i,d1[i])
    print('\n')
    for i in d2:print(i,d2[i])



if __name__ == '__main__':
    main()

I tried to make my code as clear as possible.I hope it helps.

theVoid
  • 743
  • 4
  • 14
0

Firstly find the intersection and the unions of the two dictionaries (as sets) The intersections are used for the first item of the tuple The differences are used for the second item of the tuple.

The functor is the operation to perform on dictionary items with keys from the intersection values. This is the final result used in the first tuple item.

To get the final results for the second tuple, find the merged dictionary of d1 and d2, then return only the dictionary key values with keys from the differences

def func(d1, d2, functor):
    s_intersection = set(d1) & set(d2)
    s_difference = set(d1) ^ set(d2)
    merged = d1.copy()
    merged.update(d2)
    return {k: functor(d1[k], d2[k]) for k in s_intersection}, {k: merged[k] for k in s_difference}

d1 = {1:30, 2:20, 3:30, 5:80}
d2 = {1:40, 2:50, 3:60, 4:70, 6:90}
print func(d1, d2, lambda x, y: x+y)

d1 = {1:30, 2:20, 3:30}
d2 = {1:40, 2:50, 3:60}
print func(d1, d2, lambda x, y: x>y)
Simon Black
  • 913
  • 8
  • 20