AnsweredAssumed Answered

Why can't I return a list of values NOT in a dictionary

Question asked by lindsay.raabe_FPCWA on Nov 23, 2018
Latest reply on Nov 26, 2018 by bixb0012

I have 2 dictionaries (A and B). A is a new list of keys which is being checked against B. If a key is in B but not A (is an old value), I'm attempting to get the first value from the associated key in the dictionary (in this case an OBJECTID). 

 

The issues I'm having are with line 32. As far as I can tell, this should iterate through all the keys in B, see if they're in A, and if not, write their value to a text file. As it happens, the text file is just getting the full list of old values (dictionary B) instead of only the ones not in both feature classes/dictionaries. Where are we going wrong?

 

I've attached the output text file

 

import arcpy

# Set environmental variables

arcpy.env.overwriteOutput = True

new = r"W:\Mapping\ArcGIS Online\ArcGIS Online Features.gdb\Collector_patch" #new feature class
old = r"W:\Mapping\ArcGIS Online\Upload.gdb\AGOL_Export" #old feature class
field = 'LinkKey', 'OBJECTID' #feature class fields to compare & return
fout = r"W:\Mapping\ArcGIS Online\FPC Plantations Features to Delete.txt" #output text file

a = {}
b = {}

#The old feature class is loaded into dictionary b
with arcpy.da.SearchCursor(old, (field)) as rows:
    for row in rows:
        if row[0] not in b:
            b[row[0]] = [row[1]]

#The new feature class is loaded into dictionary a
with arcpy.da.SearchCursor(new, (field)) as rows:
    for row in rows:
        if row[0] not in a:
            a[row[0]] = [row[1]]
       
#Below compares the dictionaries and extracts features with old Linkkeys"

fo = open(fout, "w")

for k, v in b.iteritems():
    if k not in a.items()[0]:
        print k
        print v
        fo.write(str(v[0]) + ',')

print "Linkkeys not in new feature class exported"

fo.close()

Attachments

Outcomes