Select to view content in your preferred language

Problem with loop in ListFields Program!!

1368
7
Jump to solution
05-23-2013 06:23 AM
MichelleCouden1
Deactivated User
My program is to find missing records when comparing to layers inside an mxd and then highlight the ones that are missing. For example, if one layer has 105H1 then the other layer should have it if not then it highlights that record to alert the user it is missing in the other layer.  Right, now it is highlighting ones that are missing but also highlighting random ones that are in both layers??? I think my problem is the loop at the bottom of my program but a) I'm not sure and b) I don't know how to fix it.

import arcpy, traceback  mxd = arcpy.mapping.MapDocument("CURRENT") lstLayers = arcpy.mapping.ListLayers(mxd)  flayer = arcpy.mapping.ListLayers(mxd, "AADT")[0] alayer = arcpy.mapping.ListLayers(mxd, "AADTAnnoLabel")[0]  FRows = arcpy.SearchCursor(flayer) ARows = arcpy.SearchCursor(alayer)  ffields = arcpy.ListFields(flayer, "FLAG", "String") afields = arcpy.ListFields(alayer, "TFLAG", "String")  FList = [] AList = []  for row in FRows:     Fvalue = row.getValue("FLAG")     FList.append(str(Fvalue))  for row in ARows:     Avalue = row.getValue("TFLAG")     AList.append(str(Avalue))  matched = set(FList) & set(AList)  for x in matched:     exp = '"FLAG" = ' + "'" + x + "'"     arcpy.SelectLayerByAttribute_management("AADT", "ADD_TO_SELECTION", exp)  arcpy.SelectLayerByAttribute_management("AADT", "SWITCH_SELECTION") 
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MichelleCouden1
Deactivated User
0 Kudos
7 Replies
RhettZufelt
MVP Notable Contributor
My program is to find missing records when comparing to layers inside an mxd and then highlight the ones that are missing. For example, if one layer has 105H1 then the other layer should have it if not then it highlights that record to alert the user it is missing in the other layer. Right, now it is highlighting ones that are missing but also highlighting random ones that are in both layers??? I think my problem is the loop at the bottom of my program but a) I'm not sure and b) I don't know how to fix it. 

import arcpy, traceback

mxd = arcpy.mapping.MapDocument("CURRENT")
lstLayers = arcpy.mapping.ListLayers(mxd)

flayer = arcpy.mapping.ListLayers(mxd, "AADT")[0]
alayer = arcpy.mapping.ListLayers(mxd, "AADTAnnoLabel")[0]

FRows = arcpy.SearchCursor(flayer)
ARows = arcpy.SearchCursor(alayer)

ffields = arcpy.ListFields(flayer, "FLAG", "String")
afields = arcpy.ListFields(alayer, "TFLAG", "String")

FList = []
AList = []

for row in FRows:
    Fvalue = row.getValue("FLAG")
    FList.append(str(Fvalue))

for row in ARows:
    Avalue = row.getValue("TFLAG")
    AList.append(str(Avalue))

matched = set(FList) & set(AList)

for x in matched:
    exp = '"FLAG" = ' + "'" + x + "'"
    arcpy.SelectLayerByAttribute_management("AADT", "ADD_TO_SELECTION", exp)

arcpy.SelectLayerByAttribute_management("AADT", "SWITCH_SELECTION","#")



Well, not sure, but see a couple things that might be an issue. I can't get switch_selection to work right without the "#" at the end.
Also, your select by attribute you are saying to select from layer AADT. How does it know what layer that is, didn't you assign it to the variable "flayer" and should use that instead.
And, even though ArcMap will let you select by attributes and export to python, selectbyattributes will not work on a feature class itself (ArcMap converts to layer on fly). Not sure how you are selecting records unless you are running this from within ArcMap, as it won't allow me to run that on a feature class in stand alone python.
To overcome this, I run a makefeaturelayer on the dataset before the selectbyattributes and switch_selection code. Then it will allow me to select by attributes, and switch.

Unfortunatly, not anywhere I can test at the moment, but thought I'd throw in a couple ideas in case it helps,

R_
0 Kudos
MichelleCouden1
Deactivated User
When I changed the AADT in the selectLayer to flayer, it gave me an error of "flayer does not exist". I think that's because I don't think I made the AADT layer named flayer, flayer was assigned to listlayers. Maybe, it gave me that error because I am running it inside the mxd with the layers it needs. I did put in the MakeFeatureLayer_management above the SelectLayers. But my result was the same. I only have 524 missing records and it is finding 878.
0 Kudos
RhettZufelt
MVP Notable Contributor
When I changed the AADT in the selectLayer to flayer, it gave me an error of "flayer does not exist". I think that's because I don't think I made the AADT layer named flayer, flayer was assigned to listlayers. Maybe, it gave me that error because I am running it inside the mxd with the layers it needs. I did put in the MakeFeatureLayer_management above the SelectLayers. But my result was the same. I only have 524 missing records and it is finding 878.


If you are running inside ArcMap, that explains how you can select on a feature class, ArcMap automatically reports it as a featurelayer to python so the MakeFeatureLayer is not needed unless you plan on running this as standalone outside ArcMap.

It looks to me that flayer is assigned to ListLayers with filter on  layer name = AADT.  So, if there is actually a layer named AADT in the TOC, it should be assigned to the flayer variable


Also, your matched = set(FList) & set(AList) will give you the values that occur in both lists, but no way to tell which list it is coming from.  So, potentially, you are trying to select something from flayer that only occurs in alayer.....

Something like below might be a better approach:

import arcpy, traceback

mxd = arcpy.mapping.MapDocument("CURRENT")

flayer = arcpy.mapping.ListLayers(mxd, "AADT")[0]
alayer = arcpy.mapping.ListLayers(mxd, "AADTAnnoLabel")[0]

FRows = arcpy.SearchCursor(flayer)
ARows = arcpy.SearchCursor(alayer)


FList = []
AList = []

for row in FRows:
    Fvalue = row.getValue("FLAG")
    FList.append(str(Fvalue))
Fset = set(FList)                                   ###  I used sets here to eliminate possible duplicate values

for row in ARows:
    Avalue = row.getValue("TFLAG")
    AList.append(str(Avalue))
Aset = set(AList)                                   ###  I used sets here to eliminate possible duplicate values


for x in Fset:
    if x not in Aset:
        exp = '"FLAG" = ' + "'" + x + "'"
        #print exp
        arcpy.SelectLayerByAttribute_management(flayer, "ADD_TO_SELECTION", exp)


Would think this would select all records in FList that match the expression and are not found in AList

Also, you might try to un-comment the print statement and make sure the exp variable is being set to a "valid" python syntax as this can sometimes be tricky in script (though prints statements really slow things down).


R_
0 Kudos
MichelleCouden1
Deactivated User
rzufelt you Rock!! Thank You so much!! So, I learned a few more things about Python today!! Thank You, for taking the time to educate and explain to me what I was doing wrong. It is greatly appreciated.  Trying to learn as fast as I can.
0 Kudos
RhettZufelt
MVP Notable Contributor
Michelle,

Glad I could help.  I'm fairly new to python also, and these forums are a great way to "learn" about it.

The more I can help someone, the more I learn as picking apart others code lets me see some of the many ways to do something in python (and there are often many ways to skin the same cat).

Also, if you get a chance, could you click the checkmark in the upper right of the post that answered your question and click the up arrow (promote) also.

This will mark the thread as answered to make it easier for others to search/find answers in the future and give me credit for answering the question as well.

R_
0 Kudos
MichelleCouden1
Deactivated User
Answered!!
0 Kudos
RhettZufelt
MVP Notable Contributor
Thanks Michelle,

However, you marked your last post as the answer which gives you the credit for it.  Should check the checkmark next to the specific post that answers the question.
(and push the up arrow to promote as well).

R_

(normally I wouldn't worry about it, but lately, I seem to be in the running )
0 Kudos