That title is a bit convoluted, but it sort of summarises what I was trying to do. I had two similar datasets, and had to return the features in the first dataset which were not in the second dataset. Spatially, this could be achieved by using the Erase tool. I used this tool often during my internship, when I created many ad hoc maps and had to create masks for various study areas.
I needed to extract a text-based list though, so I turned to set difference in Python.
|drak = [row for row in arcpy.da.SearchCursor("DSG", "SG21Code")]|
|sga = [row for row in arcpy.da.SearchCursor("SGA", "SGA.ID")]|
|lr_diff = lambda l, r: list(set(l).difference(r))|
|rest = lr_diff(sga, drak)|
|qry = """ SGA.ID IN (\'""" + "\', \'".join(rest) + "\')"|
In Lines 1 and 2, I extract all values in the field for my left and right datasets (sga and drak respectively). Those are funny names for variables, but I see that I copied and pasted my code into the gist as is, instead of changing the variable names like I normally do. I was probably under pressure (as always).
In Line 3 I define a lambda function. Now, I tend to forget these exist, especially when using ArcPy. I also am not entirely sure of when and why to use it. Nevertheless, I put one here to get the set difference between my left and right datasets i.e. all the values which only existed in sga.
I remember vividly an exercise from a GIS module in 3rd year, where instead of writing “TOWN IN (A,B,C,D)”, we were taught to write “TOWN = A OR TOWN = B OR TOWN = C OR TOWN = D”. At the time the GIS course was heavily skewed to the social sciences as those students made up the majority of the class. We were instructed to do things a certain way. It contributed to why I felt (at the time), that GIS was too easy for me, and I would rather focus on Remote Sensing. I’m really glad that I could find out for myself that things didn’t have to be that way.