为什么每个元素在for循环中发生突变后,我的Python列表中为什么都包含相同的元素?

时间:2019-05-06 11:23:02

标签: python list for-loop variable-assignment

我有一个三个字典的列表,我在for循环中更改每个字典的值。 Print语句确认每个循环已更改了其对应的dict(为不同的值,因此,在for循环之后,这些dict应该不相同)。但是,一旦我在for循环之后立即打印完整列表,该列表将包含相同的字典。我的代码如下:

split_tag = []
    for _ in split_polygon: split_tag.append(tag) # len(split_polygon) is 3
print("Initial tags: " + str(split_tag))
for i, pg in enumerate(split_polygon):
    split_tag[i]["polygon"] = pg
    split_tag[i]["bbox"] = bound_box(pg)
    print("Splitted tag " + str(i) + ": " + str(split_tag[i]))
print("Final tags: " + str(split_tag))

Python 2.7和Python 3.5都提供以下输出:

Initial tags: [{'score': 0, 'polygon': [(1668.0832177531206, 2588.404993065187), (1668.0832177531206, 3662.113730929265), (1175.9667128987517, 3662.113730929265), (1311.0385388560185, 2837.768364063218)], 'tagname': u'asphalt', 'type': 'MyPolygonItem', 'bbox': (1175.9667128987517, 2588.404993065187, 492.11650485436894, 1073.7087378640776), 'ref_id': 321745191097315359679355396732871380055L}, {'score': 0, 'polygon': [(1668.0832177531206, 2588.404993065187), (1668.0832177531206, 3662.113730929265), (1175.9667128987517, 3662.113730929265), (1311.0385388560185, 2837.768364063218)], 'tagname': u'asphalt', 'type': 'MyPolygonItem', 'bbox': (1175.9667128987517, 2588.404993065187, 492.11650485436894, 1073.7087378640776), 'ref_id': 321745191097315359679355396732871380055L}, {'score': 0, 'polygon': [(1668.0832177531206, 2588.404993065187), (1668.0832177531206, 3662.113730929265), (1175.9667128987517, 3662.113730929265), (1311.0385388560185, 2837.768364063218)], 'tagname': u'asphalt', 'type': 'MyPolygonItem', 'bbox': (1175.9667128987517, 2588.404993065187, 492.11650485436894, 1073.7087378640776), 'ref_id': 321745191097315359679355396732871380055L}]
Splitted tag 0: {'score': 0, 'polygon': [[1668, 3662], [1175, 3662], [1247, 3227], [1668, 3191]], 'tagname': u'asphalt', 'type': 'MyPolygonItem', 'bbox': (1175, 3191, 493, 471), 'ref_id': 321745191097315359679355396732871380055L}
Splitted tag 1: {'score': 0, 'polygon': [[1409, 3134], [1259, 3155], [1311, 2837], [1344, 2814], [1388, 2933], [1384, 2786], [1430, 2754]], 'tagname': u'asphalt', 'type': 'MyPolygonItem', 'bbox': (1259, 2754, 171, 401), 'ref_id': 321745191097315359679355396732871380055L}
Splitted tag 2: {'score': 0, 'polygon': [[1668, 3074], [1486, 2987], [1473, 2724], [1668, 2588]], 'tagname': u'asphalt', 'type': 'MyPolygonItem', 'bbox': (1473, 2588, 195, 486), 'ref_id': 321745191097315359679355396732871380055L}
Final tags: [{'score': 0, 'polygon': [[1668, 3074], [1486, 2987], [1473, 2724], [1668, 2588]], 'tagname': u'asphalt', 'type': 'MyPolygonItem', 'bbox': (1473, 2588, 195, 486), 'ref_id': 321745191097315359679355396732871380055L}, {'score': 0, 'polygon': [[1668, 3074], [1486, 2987], [1473, 2724], [1668, 2588]], 'tagname': u'asphalt', 'type': 'MyPolygonItem', 'bbox': (1473, 2588, 195, 486), 'ref_id': 321745191097315359679355396732871380055L}, {'score': 0, 'polygon': [[1668, 3074], [1486, 2987], [1473, 2724], [1668, 2588]], 'tagname': u'asphalt', 'type': 'MyPolygonItem', 'bbox': (1473, 2588, 195, 486), 'ref_id': 321745191097315359679355396732871380055L}]

在每个循环的末尾分别打印每个字典,表明它们确实已经突变。至少polygonbbox的值已经存在。但是,在最后打印整个列表表明,现在的字典突然相同,包括polygonbbox

有人知道我的名单被秘密更改的原因和地点吗? for循环是否可以在split_tag的某种本地版本上工作?

作为参考,我的bound__box()函数如下:

def bound_box(coordinate_pairs):
    x_min = 999999
    y_min = 999999
    x_max = 0
    y_max = 0

    for pair in coordinate_pairs:
        if pair[0] < x_min: x_min = pair[0]
        if pair[0] > x_max: x_max = pair[0]
        if pair[1] < y_min: y_min = pair[1]
        if pair[1] > y_max: y_max = pair[1]

    return (x_min, y_min, x_max - x_min, y_max - y_min)

0 个答案:

没有答案