Python使用自定义合并函数合并字典

我要合并两个字典A和B,以便结果包含:

I want to merge two dictionaries A and B such that the result contains:


  • 所有来自A的对,其中key是唯一的

  • 来自B的所有对,其中键是B的唯一

  • f(valueA,valueB) li>
  • All pairs from A where key is unique to A
  • All pairs from B where key is unique to B
  • f(valueA, valueB) where the same key exists in both A and B

例如:

def f(x, y):
    return x * y

A = {1:1, 2:3}
B = {7:3, 2:2}

C = merge(A, B)

输出:

{1:1, 7:3, 2:6}



 1 条回答