0

I am currently working with a database that hold street names, x and y coordinates and many more fields. I would like, through a search cursor, to store the x,y coordinates in a separate list for every street name (unique string).

For example, I have 23 entries under Washington street (with 23 x and 23 y coordinates). I would like to find a way to create a list named washington and store the coordinates in that list, and do that for every single street in my database inside a for loop.

For now, all I got is 1 big list with all the data :

spatialjoin = arcpy.env.workspace + '\SpatialJoin_test.shp'

coord=[]

cursor=arcpy.da.SearchCursor(spatialjoin, ['Road_Name','X','Y'])

for row in cursor:

    print(row)

    coord.append(row)
0

1 Answer 1

2

Use collections.defaultdict(list) to store your data:

import arcpy, collections

fc = r'C:\data.gdb\somefeatureclass' #Or shapefile
fields = ['Road_Name','X','Y']

d = collections.defaultdict(list)

with arcpy.da.SearchCursor(fc,fields) as cursor:
    for roadname,x,y in cursor:
        d[roadname].append([x,y])

Key will be roadname and values will be a list of lists of coordinate pairs. For example if you do:

d['Washington street']

you will get:

[[1,2],[1,4],...]

and the 21 remaining coordinate pairs.

Your next step will perhaps be to iterate over keys and values, see this question: Iterating over key and value of defaultdict dictionaries

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.