带有重复键或无的Python字典

时间:2015-02-04 15:35:02

标签: python

我在python中有一个类,我有一个包含邻居的dict,以及一个包含21个类实例的列表pri_list,但是如果我遍历邻居,我会得到重复的键或None

print type(self.neighbors)
print str(self.id) + " " + str(self.neighbors)
print ""
for n in self.neighbors:
    print str(pri_list[n].id) + " " + str(pri_list[n].neighbors)
    pri_list[n].neighbors.update({self.id:self.neighbors[n]})
    print str(pri_list[n].id) + " " + str(pri_list[n].neighbors)

导致

<type 'dict'>
21 {22: 34.348765355132869, 23: 0, 19: 0.049076419727497315, 20: 36.087031068160286}

22 {21: 34.348765355132869, 23: 34.602568871995345, 8: 0.2351466696454737, 17: 0, 20: 0}
22 {21: 34.348765355132869, 23: 34.602568871995345, 8: 0.2351466696454737, 17: 0, 20: 0}
23 {21: 0, 22: 34.602568871995345, 8: 26.643673903936367, 17: 36.80385613689073}
23 {21: 0, 21: 0, 22: 34.602568871995345, 8: 26.643673903936367, 17: 36.80385613689073}
19 {21: 0.049076419727497315, 14: 0, 18: 41.407044987390677, 20: 39.517515427920443}
19 {21: 0.049076419727497315, 14: 0, 18: 41.407044987390677, 20: 39.517515427920443}
20 {21: 36.087031068160286, 22: 0, 18: 0.58414114578772669, 19: 39.517515427920443}
20 {21: 36.087031068160286, 22: 0, 18: 0.58414114578772669, 19: 39.517515427920443}

观察更新后密钥21在id = 23 dict中如何出现两次。

我试过了

pri_list[n].neighbors[self.id] = self.neighbors[n]

给出相同的结果。

我在两台不同的机器上试过它,结果相同。

我做了一个测试示例,我将dict中的数据复制为。

class A:
    neighbors = {}
    id = 0

    def __init__(self,id, neighbors):
        self.id = id
        self.neighbors = neighbors.copy()

if __name__ == "__main__":
    pri_list = [A(i, {})for i in range(0, 19)]

    pri_list.append(A(19, {21: 0.049076419727497315, 14: 0, 18: 41.407044987390677, 20: 39.517515427920443}))
    pri_list.append(A(20, {21: 36.087031068160286, 22: 0, 18: 0.58414114578772669, 19: 39.517515427920443}))
    pri_list.append(A(21, {22: 34.348765355132869, 23: 0, 19: 0.049076419727497315, 20: 36.087031068160286}))
    pri_list.append(A(22, {21: 34.348765355132869, 23: 34.602568871995345, 8: 0.2351466696454737, 17: 0, 20: 0}))
    pri_list.append(A(23, {21: 0, 22: 34.602568871995345, 8: 26.643673903936367, 17: 36.80385613689073}))

    print type(pri_list[21].neighbors)
    print str(pri_list[21].id) + " " + str(pri_list[21].neighbors)
    print ""

    for n in pri_list[21].neighbors:
        print str(pri_list[n].id) + " " + str(pri_list[n].neighbors)
        pri_list[n].neighbors.update({pri_list[21].id:pri_list[21].neighbors[n]})
        print str(pri_list[n].id) + " " + str(pri_list[n].neighbors)

这给出了预期的结果

<type 'dict'>
21 {22: 34.348765355132869, 23: 0, 19: 0.049076419727497315, 20:     36.087031068160286}

22 {8: 0.2351466696454737, 17: 0, 20: 0, 21: 34.348765355132869, 23: 34.602568871995345}
22 {8: 0.2351466696454737, 17: 0, 20: 0, 21: 34.348765355132869, 23: 34.602568871995345}
23 {21: 0, 22: 34.602568871995345, 8: 26.643673903936367, 17: 36.80385613689073}
23 {21: 0, 22: 34.602568871995345, 8: 26.643673903936367, 17: 36.80385613689073}
19 {21: 0.049076419727497315, 14: 0, 18: 41.407044987390677, 20: 39.517515427920443}
19 {21: 0.049076419727497315, 14: 0, 18: 41.407044987390677, 20: 39.517515427920443}
20 {21: 36.087031068160286, 22: 0, 18: 0.58414114578772669, 19: 39.517515427920443}
20 {21: 36.087031068160286, 22: 0, 18: 0.58414114578772669, 19: 39.517515427920443}

观察更新后密钥21在id=23 dict中仅出现一次的情况,并且字典的排列也不同。

我很难理解同一个词组中多个键的出现方式,根据https://docs.python.org/2/library/stdtypes.html#dict这不应该是可能的

1 个答案:

答案 0 :(得分:0)

您提供的代码存在的问题是您更新字典同时对其进行迭代。我不能100%地确定这是原因,但是通常这是一个坏主意,并且确实会导致不可预测的结果。只需复制一份即可遍历。