BBG_GIS
BBG_GIS

Reputation: 305

How to iterate variable names in python and set the values to them?

I want to iterate a method (union) to union the records with geom variables and put the result to another variable(ex: g variable).I wrote The snippet code for two records (geom1,geom2) but don't know how to iterate the variable names and values.

import arcpy
# Spatial reference of input feature class
SR = arcpy.SpatialReference(4326)
InFc=r"D:\_GIS\VerticesToLine.gdb\Line"
output = r"D:\_GIS\VerticesToLine.gdb\test"
geom = [row[0] for row in arcpy.da.SearchCursor(InFc, ["SHAPE@"])]
geom[0].union(geom[1])
g= geom[2].union(geom[0])
with arcpy.da.InsertCursor(output, ["SHAPE@"]) as cur:
    cur.insertRow((g,))

Upvotes: 0

Views: 574

Answers (1)

Patrick Haugh
Patrick Haugh

Reputation: 60944

Keep the list instead of unpacking it.

geom = [row[0] for row in arcpy.da.SearchCursor(InFc, ["SHAPE@"])]
geom[0].union(geom[1])
g= geom[2].union(geom[0])
with arcpy.da.InsertCursor(output, ["SHAPE@"]) as cur:
for geo in geom:
    cur.insertRow((g,))

Are you really trying to insertRow((g,)) three times, or should that be geo?

If you want to union together an arbitrary number of recodrs, you can do:

geom = [row[0] for row in arcpy.da.SearchCursor(InFc, ["SHAPE@"])]
g = geom[0]
for geo in geom[1:]:
    g = g.union(geo)

Upvotes: 1

Related Questions