创建新列表时覆盖现有列表
Existing list overwritten when creating a new list
在下面的脚本中,当第二个循环开始时会发生一些奇怪的事情。列表 area_1
更改值,即使它根本不参与该循环。在第三个循环之后 area_2
也会发生同样的情况。
server_1 = 'x1'
server_2 = 'x2'
server_3 = 'x3'
table = [
{'Terminal': 'site_1', 'City': 'London', 'Station': server_1},
{'Terminal': 'site_2', 'City': 'London', 'Station': server_1},
{'Terminal': 'site_3', 'City': 'London', 'Station': server_2},
{'Terminal': 'site_4', 'City': 'New York', 'Station': server_3},
{'Terminal': 'site_5', 'City': 'New York', 'Station': server_1},
{'Terminal': 'site_6', 'City': 'New York','Station': server_2},
{'Terminal': 'site_7', 'City': 'Tokyo', 'Station': server_3},
{'Terminal': 'site_8', 'City': 'Tokyo', 'Station': server_3},
{'Terminal': 'site_9', 'City': 'Tokyo', 'Station': server_2}
]
city_index = [
{'City':'London', 'Lat': 51.48,'Long': -0.14 },
{'City':'New York', 'Lat': 40.69,'Long': -74.26 },
{'City':'Tokyo', 'Lat': 35.51,'Long': 138.64 }
]
area_1 = []
for i in city_index:
area_1.append(i)
area_1[-1].update({'Station': server_1, 'Count of terminals': sum(q['City'] == i['City'] and q['Station'] == server_1 for q in table)})
area_2 = []
for i in city_index:
area_2.append(i)
area_2[-1].update({'Station': server_2, 'Count of terminals': sum(q['City'] == i['City'] and q['Station'] == server_2 for q in table)})
area_3 = []
for i in city_index:
area_3.append(i)
area_3[-1].update({'Station': server_3, 'Count of terminals': sum(q['City'] == i['City'] and q['Station'] == server_3 for q in table)})
all = area_1 + area_2 + area_3
for i in all:
print(i)
输出:(注意站键)
{'City': 'London', 'Lat': 51.48, 'Long': -0.14, 'Station': 'x3', 'Count of terminals': 0}
{'City': 'New York', 'Lat': 40.69, 'Long': -74.26, 'Station': 'x3', 'Count of terminals': 1}
{'City': 'Tokyo', 'Lat': 35.51, 'Long': 138.64, 'Station': 'x3', 'Count of terminals': 2}
{'City': 'London', 'Lat': 51.48, 'Long': -0.14, 'Station': 'x3', 'Count of terminals': 0}
{'City': 'New York', 'Lat': 40.69, 'Long': -74.26, 'Station': 'x3', 'Count of terminals': 1}
{'City': 'Tokyo', 'Lat': 35.51, 'Long': 138.64, 'Station': 'x3', 'Count of terminals': 2}
{'City': 'London', 'Lat': 51.48, 'Long': -0.14, 'Station': 'x3', 'Count of terminals': 0}
{'City': 'New York', 'Lat': 40.69, 'Long': -74.26, 'Station': 'x3', 'Count of terminals': 1}
{'City': 'Tokyo', 'Lat': 35.51, 'Long': 138.64, 'Station': 'x3', 'Count of terminals': 2}
所有 3 个列表在最后变得相同 = area_3
。
有人可以解释为什么会这样吗?为什么 area_1
和 area_2
被覆盖了?
area_1
和 area_2
没有被覆盖。实际被覆盖的是city_index
。完成所有操作后尝试打印它,您就会看到。
为避免这种情况,您应该复制 city_index
的内容,而不是覆盖 (dict.update(...)
) 它们。
实现此目的的一种方法是找到您所做的任何地方:
area_x.append(i)
改为
area_x.append(dict(**i)) # or area_x.append(dict(i))
这将强制复制 i
而不是引用。
使用 copy
模块复制对象而不是引用它们。
import copy
area.append(copy.copy(i))
另外,使用i
作为非整数的迭代器很容易混淆,尽量避免
在下面的脚本中,当第二个循环开始时会发生一些奇怪的事情。列表 area_1
更改值,即使它根本不参与该循环。在第三个循环之后 area_2
也会发生同样的情况。
server_1 = 'x1'
server_2 = 'x2'
server_3 = 'x3'
table = [
{'Terminal': 'site_1', 'City': 'London', 'Station': server_1},
{'Terminal': 'site_2', 'City': 'London', 'Station': server_1},
{'Terminal': 'site_3', 'City': 'London', 'Station': server_2},
{'Terminal': 'site_4', 'City': 'New York', 'Station': server_3},
{'Terminal': 'site_5', 'City': 'New York', 'Station': server_1},
{'Terminal': 'site_6', 'City': 'New York','Station': server_2},
{'Terminal': 'site_7', 'City': 'Tokyo', 'Station': server_3},
{'Terminal': 'site_8', 'City': 'Tokyo', 'Station': server_3},
{'Terminal': 'site_9', 'City': 'Tokyo', 'Station': server_2}
]
city_index = [
{'City':'London', 'Lat': 51.48,'Long': -0.14 },
{'City':'New York', 'Lat': 40.69,'Long': -74.26 },
{'City':'Tokyo', 'Lat': 35.51,'Long': 138.64 }
]
area_1 = []
for i in city_index:
area_1.append(i)
area_1[-1].update({'Station': server_1, 'Count of terminals': sum(q['City'] == i['City'] and q['Station'] == server_1 for q in table)})
area_2 = []
for i in city_index:
area_2.append(i)
area_2[-1].update({'Station': server_2, 'Count of terminals': sum(q['City'] == i['City'] and q['Station'] == server_2 for q in table)})
area_3 = []
for i in city_index:
area_3.append(i)
area_3[-1].update({'Station': server_3, 'Count of terminals': sum(q['City'] == i['City'] and q['Station'] == server_3 for q in table)})
all = area_1 + area_2 + area_3
for i in all:
print(i)
输出:(注意站键)
{'City': 'London', 'Lat': 51.48, 'Long': -0.14, 'Station': 'x3', 'Count of terminals': 0}
{'City': 'New York', 'Lat': 40.69, 'Long': -74.26, 'Station': 'x3', 'Count of terminals': 1}
{'City': 'Tokyo', 'Lat': 35.51, 'Long': 138.64, 'Station': 'x3', 'Count of terminals': 2}
{'City': 'London', 'Lat': 51.48, 'Long': -0.14, 'Station': 'x3', 'Count of terminals': 0}
{'City': 'New York', 'Lat': 40.69, 'Long': -74.26, 'Station': 'x3', 'Count of terminals': 1}
{'City': 'Tokyo', 'Lat': 35.51, 'Long': 138.64, 'Station': 'x3', 'Count of terminals': 2}
{'City': 'London', 'Lat': 51.48, 'Long': -0.14, 'Station': 'x3', 'Count of terminals': 0}
{'City': 'New York', 'Lat': 40.69, 'Long': -74.26, 'Station': 'x3', 'Count of terminals': 1}
{'City': 'Tokyo', 'Lat': 35.51, 'Long': 138.64, 'Station': 'x3', 'Count of terminals': 2}
所有 3 个列表在最后变得相同 = area_3
。
有人可以解释为什么会这样吗?为什么 area_1
和 area_2
被覆盖了?
area_1
和 area_2
没有被覆盖。实际被覆盖的是city_index
。完成所有操作后尝试打印它,您就会看到。
为避免这种情况,您应该复制 city_index
的内容,而不是覆盖 (dict.update(...)
) 它们。
实现此目的的一种方法是找到您所做的任何地方:
area_x.append(i)
改为
area_x.append(dict(**i)) # or area_x.append(dict(i))
这将强制复制 i
而不是引用。
使用 copy
模块复制对象而不是引用它们。
import copy
area.append(copy.copy(i))
另外,使用i
作为非整数的迭代器很容易混淆,尽量避免