Adding to a tuple overwriting previous values

I use arcpy to get all the polylines of a form file. SearchCursor returns a cursor so that I can iterate through all the functions of the form file. The problem is that I want to keep all the objects returned by the cursor for later use.

 import arcpy from arcpy import env env.workspace = r"C:\GIS Data\GIS data" desc = arcpy.Describe("River.shp") shapefieldname = desc.ShapeFieldName rows = arcpy.SearchCursor("River.shp") featureList = () for row in rows: feat = row.getValue(shapefieldname) featureList = featureList + (feat, ) print "%i %i" % (featureList[-1].firstPoint.X, featureList[-1].firstPoint.Y) print "%i %i" % (featureList[-1].lastPoint.X, featureList[-1].lastPoint.Y) print print "---------------------------------------------------------------" for feat in featureList: print "%i %i" % (feat.firstPoint.X, feat.firstPoint.Y) print "%i %i" % (feat.lastPoint.X, feat.lastPoint.Y) print 

Tuple must contain all objects returned by the cursor. But it has only the last elements that are repeated by the size number of tuples of times.

3610930 2135882 3611593 2134453

3611806 2134981 3611593 2134453

3614160 2136164 3617432 2131734

3611593 2134453 3617432 2131734

3617432 2131734 3620568 2127591

3620568 2127591 3620785 2127423

3617980 2126657 3620568 2127591

3616768 2129454 3617948 2126649

3617948 2126649 3617980 2126657

3615102 2128889 3617587 2126510

3617587 2126510 3617948 2126649

3617624 2126416 3617980 2126657

3613129 2128176 3615155 2125617

3615155 2125617 3617587 2126510

3615086 2125515 3615155 2125617


3615086 2125515 3615155 2125617

3615086 2125515 3615155 2125617

3615086 2125515 3615155 2125617

3615086 2125515 3615155 2125617

3615086 2125515 3615155 2125617

3615086 2125515 3615155 2125617

3615086 2125515 3615155 2125617

3615086 2125515 3615155 2125617

3615086 2125515 3615155 2125617

3615086 2125515 3615155 2125617

3615086 2125515 3615155 2125617

3615086 2125515 3615155 2125617

3615086 2125515 3615155 2125617

3615086 2125515 3615155 2125617

3615086 2125515 3615155 2125617

At first I tried to use this list. The same output was found for the list when I used the append () method. As a tuple is an immutable data structure, as + overwrites all previous elements of the tuple. Although this code is written for the arc, but I think the problem is not specific.

+3
source share
2 answers

This suggests that row.getValue() continues to return references to the same object, which it constantly updates.

To check, try typing id(feat) , id(feat.firstPoint) and id(feat.lastPoint) in the first loop and see if one of the identifiers remains the same between iterations. If either of them does this, your problem.

As a tuple is an immutable data structure, how can + overwrite all previous elements of the tuple.

This is not true. Tuple is immutable in the sense that you cannot add or remove elements from it without creating a new tuple. You also cannot change the value of a tuple element. However, if this element is a reference to a mutable object, you can change the object itself. This is what I suspect is happening here: you have multiple references to the same object; when one of them changes, they all change.

+4
source

You can do this as a one-line understanding:

 import arcpy from arcpy import env env.workspace = r"C:\GIS Data\GIS data" desc = arcpy.Describe("River.shp") shapefieldname = desc.ShapeFieldName rows = arcpy.SearchCursor("River.shp") feature_list = [row.getValue( shapefieldname ) for row in rows] 

In this example, feature_list will be a list of the values โ€‹โ€‹of your form field.

0
source

All Articles