Prevent Python For Loop from overwriting dictionary

Prevent Python For Loop from overwriting dictionary



I am trying to create a new dictionary (intersections) that contains intersecting areas from the polygons in a dictionary called zones.


intersections


zones



I use combinations to find all possible unique combinations of zones. Then I use Shapely's .intersects() to test if the zones intersect. If they do so, I would like their geometry being saved in variable int_geometry and then be stored in a dictionary (using: Shapely's .intersection()).


combinations


zones


.intersects()


if


int_geometry


.intersection()



I know there are four intersections, because this code returns them:


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)



However, if I replace what comes after the if statement like in the code below, it starts overwriting itself.


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)



There are multiple topics on similar issues, though I cannot find my answer. I know I am overlooking something very obvious here, but I can't detect it. Can someone explain to me what I'm doing wrong?






You are reassigning the name intersections in each iteration of the for loop to a new dictionary. (The one you build on the right hand side.) It's hard to say more because your code is not self contained (we can't copy paste and run it) and the final desired outcome is unclear.

– timgeb
Sep 8 '18 at 10:49



intersections


for






You probably want something like <code> intersections[geometry] = 'geometry' : int_geometry, 'attributes' : 'area' : int_area, 'perimeter' : int_perimeter, </code> instead.

– chai
Sep 8 '18 at 11:02


intersections[geometry] = 'geometry' : int_geometry, 'attributes' : 'area' : int_area, 'perimeter' : int_perimeter,




1 Answer
1



index in int_index = "int_index " + str(index + 1) will always be the same value as after first loop it's value is constant. Hence data is being overwritten.


index


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)






Yes, I would like to store the data, like in your first example and I know how to store the data in a list, but I would like to store it in a dictionary. I just added my full code above. As you can see I (think I) use exactly the same structure for creating the zones dictionary. And that works perfectly fine.

– Graven
Sep 8 '18 at 11:07


zones






index in int_index = "int_index " + str(index + 1) will always be the same value as after first loop it's value is constant. Hence overwriting.

– Himavanth
Sep 8 '18 at 11:28


index


int_index = "int_index " + str(index + 1)



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ

Node.js puppeteer - Use values from array in a loop to cycle through pages