循环中的静态值覆盖字典分配

时间:2018-11-16 20:28:46

标签: loops dictionary

我正在尝试计算两个单独列表的因子之间的绝对差。经过交叉计算,思考的过程是将它们保存到整体词典“ kk”中。

kk  = {}
ks = [4,5]
ks2 = [1,2,3,4]
for i in ks:
    diff = {abs(i-j):j  for j in ks2}

    kk[i] = diff

这就是我得到的:

{4: {1: 4, 2: 3, 3: 2, 4: 1}, 5: {1: 4, 2: 3, 3: 2, 4: 1}}

我们可以看到,这是不正确的,因为两个键的“ kk”值相同...

0 个答案:

没有答案