1

I have a list of feature classes (fcList) that I would like to do the following. - add a field (fire_dis) - update row from a single value from a dictionary(fcnaDict) - more specifically I am trying to run this through a loop so that the first item in the fcList will get the first dictionary value "Livingston Fire" and so on until every fc has the reciprocal dictionary value.

Here is my code. FYI I am quite new at python.

a = arcpy.MakeFeatureLayer_management(outFeatureClass, "LivingstonFire", CLF)
b = arcpy.MakeFeatureLayer_management(outFeatureClass, "GardinerFD_GatewayHoseCo", GFDGHC)
c = arcpy.MakeFeatureLayer_management(outFeatureClass, "CookeCitySilverGateVFD", CCSVFD)
d = arcpy.MakeFeatureLayer_management(outFeatureClass, "ClydeParkCity", CPC)
e = arcpy.MakeFeatureLayer_management(outFeatureClass, "ClydeParkRFD", CPRFD)
f = arcpy.MakeFeatureLayer_management(outFeatureClass, "WilsallRFD", WRFD)
g = arcpy.MakeFeatureLayer_management(outFeatureClass, "ParkCountyRFD1", PCRFD1) 
h = arcpy.MakeFeatureLayer_management(outFeatureClass, "ParadiseValleyFSA", PVFSA) 
i = arcpy.MakeFeatureLayer_management(outFeatureClass, "CountyProtection", CoPro)
j = arcpy.MakeFeatureLayer_management(outFeatureClass, "NullParcels", Null)

a2 = arcpy.Dissolve_management(a, str(a), "", "", "MULTI_PART","")
b2 = arcpy.Dissolve_management(b, str(b), "", "", "MULTI_PART","")
c2 = arcpy.Dissolve_management(c, str(c), "", "", "MULTI_PART","")
d2 = arcpy.Dissolve_management(d, str(d), "", "", "MULTI_PART","")
e2 = arcpy.Dissolve_management(e, str(e), "", "", "MULTI_PART","")
f2 = arcpy.Dissolve_management(f, str(f), "", "", "MULTI_PART","")
g2 = arcpy.Dissolve_management(g, str(g), "", "", "MULTI_PART","")
h2 = arcpy.Dissolve_management(h, str(h), "", "", "MULTI_PART","")
i2 = arcpy.Dissolve_management(i, str(i), "", "", "MULTI_PART","")
j2 = arcpy.Dissolve_management(j, str(j), "", "", "MULTI_PART","")
#Dissolve the exempt layers from above
k2 = arcpy.Dissolve_management(OtherExlayer, "OtherExparcels","", "", "MULTI_PART","")
l2 = arcpy.Dissolve_management(FSlayer, "FSparcels","", "", "MULTI_PART","")
m2 = arcpy.Dissolve_management(DNRClayer, "DNRCparcels","", "", "MULTI_PART","")    


## This is my dictionary
fcnaDict = { "a2":"Livingston Fire", "b2":"Gardiner Fire Department & Gateway Hose Co",
"c2":"Cooke City Silver Gate VFD", "d2":"Clyde Park City", "e2":"Clyde Park RFD", 
"f2":"Wilsall RFD", "g2":"Park County RFD1", "h2":"Paradise Valley FSA",
"i2":"County Protection", "j2":"Null Parcels", "k2":"Other Exparcels",
"l2":"FS parcels", "m2":"DNRC parcels"}

## This is my list of objects for feature classes
fcList = [ a2, b2, c2, d2, e2, f2, g2, h2, i2, j2, k2, l2, m2]

## This is my loop with the above mentioned items
for fc in fcList:
    arcpy.AddField_management( fc,"fire_dis", "TEXT", 100)
    cursor = arcpy.da.UpdateCursor(fc,"fire_dis",)
    for row in cursor:
        row[0] = fcnaDict.values()
        cursor.updateRow(row)

The error I am getting now is that updateRow does not support lists.

  • 1
    Probably just a pasting error, but there is a dangling '(' in: row[0] = fcnaDict.values( – phloem Jan 2 '15 at 19:16
  • @Aaron I was suggested to use that. How could I script this with a list instead? – Lotalota Jan 2 '15 at 21:30
  • @phloem pasting error was on stack not in my script, good catch though – Lotalota Jan 2 '15 at 21:32
  • After looking at this more, you are right in that a dictionary is a good way to go (answer below). – Aaron Jan 2 '15 at 22:18
  • Are you open to a completely different approach -- if so, consider making this into a function, avoiding the need for a dictionary at all and making the code more condensed and legible. – Erica Jan 2 '15 at 22:37
3

using .values will return the full dictionnary. What you need is :

row[0] = fcnaDict[fc]

This will copy a single value in all row for each feature class. Note that I would suggest you to put all the feature classes in the same feature class in your case, but you probably have good reasons not to do this.

note that your feature class list should contain text, because you cannot convert your object names to string (from what I know). so you might need to use some tricks to link your objects and your keys, but it starts with the way you name your keys and your feature classes (everything would be nice and clear if they had the same names).

EDIT: example with a dictionnary

## This is my list of objects for feature classes
fcList = [ a2, b2, c2, d2, e2, f2, g2, h2, i2, j2, k2, l2, m2]

## This is my loop with the above mentioned items
for j in range(13):
    arcpy.AddField_management( fcList[j],"fire_dis", "TEXT", 100)
    cursor = arcpy.da.UpdateCursor(fcList[j],"fire_dis",)
    for row in cursor:
        row[0] = fcnaDict[chr(97+j) + "2")] #create a key value , chr(97) returns "a", and so on for "b", "c" etc
        cursor.updateRow(row)

EDIT : example with a list, the order when you write the list is now important

fcnaList = [ "Livingston Fire", "Gardiner Fire Department & Gateway Hose Co",
    "Cooke City Silver Gate VFD", "Clyde Park City", "Clyde Park RFD", 
    "Wilsall RFD", "Park County RFD1", "Paradise Valley FSA",
    "County Protection", "Null Parcels", "Other Exparcels",
    "FS parcels", "DNRC parcels" ]
## This is my loop with the above mentioned items
for j in range(13):
    arcpy.AddField_management( fcList[j],"fire_dis", "TEXT", 100)
    cursor = arcpy.da.UpdateCursor(fcList[j],"fire_dis",)
    for row in cursor:
        row[0] = fcnaList[j] #get the jth item in the list when you pick the jth feature class
        cursor.updateRow(row)
  • have you tried my last edit. As I said, you cannot get a string from the name of an object. I you don't provide information about your choices of object names and feature class names, so it is hard to guess. However, what I propose should work in any case, you just need two lists (or one list and ond dictionnary): one with the keys, and one with the objects. You need to type those list, so please try to select names that are easy to work with. – radouxju Jan 2 '15 at 21:56
  • I'm working on it now. FYI I updated my code above to help you understand my object name choices. – Lotalota Jan 2 '15 at 22:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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