ArcMap10.1 Loop through a unique list to create new layers for each unique name.

1688
13
Jump to solution
04-10-2014 07:50 AM
StewartChritton
New Contributor
I have only taken one GIS class that used python so this is messy!

I was able to get the unique list from the attributes table under the column 'Layer'. Next I need the program to use each unique name from that list, select by attributes and create a new layer file for each unique name. I have the count in there because there are 33 different unique names in that list. I know there is a better way to do this, I just don't remember how.

I realize my code is far from perfect so if you have other questions or tips please don't hesitate to ask.

import arcpy, sys, os, traceback

fc = r"C:\Users\Drew\Desktop\Montpelier\PythonTest\PythonGDB.gdb\dshline_Project"
field = ["Layer"]
valueList = []

#Use search cursor to aquire a list of unique values
    with arcpy.da.SearchCursor(fc, field) as cursor:
        for row in cursor:
            valueList.append(row[0])

    uniqueSet = set(valueList)
    uniqueList = list(uniqueSet)
    uniqueList.sort()

    del cursor

    count = 0
    name = uniqueList[count]
    outputname = r"C:\Users\Drew\Desktop\Montpelier\PythonTest\PythonGDB.gdb\"" + name
    if count < 34:
        arcpy.MakeFeatureLayer_management (r"C:\Users\Drew\Desktop\Montpelier\PythonTest\PythonGDB.gdb\dshline_Project", "layer")
        arcpy.SelectLayerByAttribute_management("layer","NEW_SELECTION", """Layer" = """ + name)


##        arcpy.SelectLayerByAttribute_management("dshline_Project","NEW_SELECTION",""""Layer" = 'ANNOT'""")

        arcpy.CopyFeatures_management(fc,outputname,"#","0","0","0")
        count += 1
    else:
        print "Done"

Please Help!
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor
Try the following for the Select function:

arcpy.SelectLayerByAttribute_management("layer", "NEW_SELECTION", "Layer = '" + name + "'")

View solution in original post

0 Kudos
13 Replies
JakeSkinner
Esri Esteemed Contributor
Try the following:

import arcpy, sys, os, traceback
from arcpy import env
env.workspace = r"C:\Users\Drew\Desktop\Montpelier\PythonTest\PythonGDB.gdb"
env.overwriteOutput = 1

fc = "dshline_Project"
field = ["Layer"]
valueList = []

#Use search cursor to aquire a list of unique values
with arcpy.da.SearchCursor(fc, field) as cursor:
    for row in cursor:
    valueList.append(row[0])

uniqueSet = set(valueList)
uniqueList = list(uniqueSet)
uniqueList.sort()

del cursor

count = 0
name = uniqueList[count]

if count < 34:
    outputname = env.workspace + os.sep + name
    arcpy.MakeFeatureLayer_management (fc, "layer")
    arcpy.SelectLayerByAttribute_management("layer", "NEW_SELECTION", "Layer = " + name)
    arcpy.CopyFeatures_management("layer", outputname)
    count += 1
    name = uniqueList[count]
else:
    print "Done"
0 Kudos
StewartChritton
New Contributor
Thank you for your quick response.

I am still getting an error with the SelectLayerByAttribute:

ERROR 000358: Invalid expression
Failed to execute (SelectLayerByAttribute).

I feel like it has to be the following bolded part: arcpy.SelectLayerByAttribute_management("layer", "NEW_SELECTION", "Layer = " + name)
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Try the following for the Select function:

arcpy.SelectLayerByAttribute_management("layer", "NEW_SELECTION", "Layer = '" + name + "'")
0 Kudos
StewartChritton
New Contributor
Partial success!! It created the first file in the list successfully but didn't continue on to do the rest. Something wrong with the way i set up the loop perhaps?

Also just noticed that it doesn't print done in the end...
0 Kudos
StewartChritton
New Contributor
I just changed the loop to a while loop.

    lencount = len(uniqueList)

    while count < lencount:

Now it is working but I get an error at the end:

Traceback info:
  File "C:\Users\Drew\Desktop\Montpelier\PythonTest\ep2.py", line 41, in <module>
    name = uniqueList[count]

Error Info:
list index out of range

ArcPy ERRORS:

I think it keeps trying to go after the loop should close but it already accomplished what I needed so i guess that's fine!


Thanks a bunch for you fast replies!
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Glad this is working.  Can you mark this forum as answered?  It will further help users in the community.
0 Kudos
StewartChritton
New Contributor
Of course, I cannot find the button to mark it... one last bit of help please?
0 Kudos
T__WayneWhitley
Frequent Contributor
Yes, sloppy it is, and although it finishes maybe you still want to know why it's behaving that way?

It's because you make it through the loop on the last member of the list, then increment your index variable (i) and try to set your variable by looking up the value by index - the result is it does not exist because you have in fact exceeded the range, hence the 'out of range' error.  The index of a list is zero-based, e.g., your list index with 33 members must advance in the loop from i = 0 to i = 32....in other words, 0 to 32 (including the 0) is 33.

I suppose the easiest fix is to modify how you enter the loop:
while count <= lencount - 1:
     # the rest of your code
     # so that 33 - 1 = 32, the upper index 0-based limit



Wayne
0 Kudos
StewartChritton
New Contributor
Thanks! That makes perfect sense.
0 Kudos