防止 Python For 循环覆盖字典
Prevent Python For Loop from overwriting dictionary
我正在尝试创建一个新字典 (intersections
),其中包含名为 zones
.
的字典中多边形的交叉区域
我使用 combinations
来查找 zones
的所有可能的唯一组合。然后我使用 Shapely 的 .intersects()
来测试 if
区域相交。如果他们这样做,我希望将他们的几何图形保存在变量 int_geometry
中,然后存储在字典中(使用:Shapely 的 .intersection()
)。
我知道有四个个交集,因为这段代码returns他们:
for a, b in combinations(zones.values(), 2):
a_geom = a['location']
b_geom = b['location']
if a_geom.intersects(b_geom) == True:
print a_geom.intersection(b_geom)
但是,如果我像下面的代码一样替换 if
语句之后的内容,它就会开始自我覆盖。
intersections = {}
for a, b in combinations(zones.values(), 2):
a_geom = a['location']
b_geom = b['location']
if a_geom.intersects(b_geom) == True:
int_geometry = a_geom.intersection(b_geom)
int_area = round(int_geometry.area,2)
int_perimeter = round(int_geometry.length,2)
intersections = {
'geometry' : int_geometry,
'attributes' : {
'area' : int_area,
'perimeter' : int_perimeter,
}
}
pprint(intersections)
类似问题有多个主题,但我找不到答案。我知道我在这里忽略了一些非常明显的东西,但我无法检测到它。有人可以向我解释我做错了什么吗?
index
in int_index = "int_index " + str(index + 1)
将始终与第一个循环后的值相同,它的值是常量。因此数据正在被覆盖。
...
# PART 2 - ANALYSE THE DATA
intersections = {}
index = 0
for a, b in combinations(zones.values(), 2):
a_geom = a['location']
b_geom = b['location']
if a_geom.intersects(b_geom) == True:
int_index = "int_index " + str(index + 1)
int_geometry = a_geom.intersection(b_geom)
int_area = round((a_geom.intersection(b_geom).area),2)
int_perimeter = round((a_geom.intersection(b_geom).length),2)
intersections[int_index] = {
'geometry' : int_geometry,
'attributes' : {
'area' : int_area,
'perimeter' : int_perimeter,
}
}
index += 1
pprint(intersections)
我正在尝试创建一个新字典 (intersections
),其中包含名为 zones
.
我使用 combinations
来查找 zones
的所有可能的唯一组合。然后我使用 Shapely 的 .intersects()
来测试 if
区域相交。如果他们这样做,我希望将他们的几何图形保存在变量 int_geometry
中,然后存储在字典中(使用:Shapely 的 .intersection()
)。
我知道有四个个交集,因为这段代码returns他们:
for a, b in combinations(zones.values(), 2):
a_geom = a['location']
b_geom = b['location']
if a_geom.intersects(b_geom) == True:
print a_geom.intersection(b_geom)
但是,如果我像下面的代码一样替换 if
语句之后的内容,它就会开始自我覆盖。
intersections = {}
for a, b in combinations(zones.values(), 2):
a_geom = a['location']
b_geom = b['location']
if a_geom.intersects(b_geom) == True:
int_geometry = a_geom.intersection(b_geom)
int_area = round(int_geometry.area,2)
int_perimeter = round(int_geometry.length,2)
intersections = {
'geometry' : int_geometry,
'attributes' : {
'area' : int_area,
'perimeter' : int_perimeter,
}
}
pprint(intersections)
类似问题有多个主题,但我找不到答案。我知道我在这里忽略了一些非常明显的东西,但我无法检测到它。有人可以向我解释我做错了什么吗?
index
in int_index = "int_index " + str(index + 1)
将始终与第一个循环后的值相同,它的值是常量。因此数据正在被覆盖。
...
# PART 2 - ANALYSE THE DATA
intersections = {}
index = 0
for a, b in combinations(zones.values(), 2):
a_geom = a['location']
b_geom = b['location']
if a_geom.intersects(b_geom) == True:
int_index = "int_index " + str(index + 1)
int_geometry = a_geom.intersection(b_geom)
int_area = round((a_geom.intersection(b_geom).area),2)
int_perimeter = round((a_geom.intersection(b_geom).length),2)
intersections[int_index] = {
'geometry' : int_geometry,
'attributes' : {
'area' : int_area,
'perimeter' : int_perimeter,
}
}
index += 1
pprint(intersections)