>>> # state at one point of the selection >>> listOfOIDs1 = [1,2,3,4] >>> # state after adding to the selection (5 added) >>> listOfOIDs2 = [1,2,3,4,5] >>> theNewOID = [oid for oid in listOfOIDs2 if oid not in listOfOIDs1] >>> print theNewOID [5] >>> >>> # EDIT 2:18 PM >>> # ...or this, to compare lists: >>> set(listOfOIDs2).difference(listOfOIDs1).pop() 5
import arcpy import pythonaddins lstChanged = [] fc = r"C:\path\to\layer\Lots" field = "OBJECTID" class CheckSelectionChange(object): """Implementation for PyAddInWizard_addin.ext1 (Extension)""" def __init__(self): # For performance considerations, please remove all unused methods in this class. self.enabled = True def onEditorSelectionChanged(self): fc = r"C:\Users\gkeith\Documents\Projects\Laurie\LaurieTest.gdb\Lots" field = "OBJECTID" with arcpy.SearchCursor(fc, field) as row: if row.field in lstChanged: lstChanged.pop(row.field) # this may be unnecessary, but will try it else: lstChanged.append(row.field) class CreateLotNum(object): """Implementation for PyAddInWizard_addin.btn1 (Button)""" def __init__(self): self.enabled = True self.checked = False def onClick(self): # button event handler lstDoubleCheck = [] with arcpy.SearchCursor(fc, field) as row: lstDoubleCheck.append(row.field) # plan to check lstDoubleCheck, all currently selected parcels, against lstChanged, any that were listed in onEditor...
import arcpy import pythonaddins lstChanged = [] fc = r"C:\path\to\file\Lots" field = "OBJECTID" class CheckSelectionChange(object): """Implementation for PyAddInWizard_addin.ext1 (Extension)""" def __init__(self): # For performance considerations, please remove all unused methods in this class. self.enabled = True def onEditorSelectionChanged(self): for row in arcpy.SearchCursor(fc, '', '', field): print "Row: " + str(row.OBJECTID) + "\n" # just for testing if row.OBJECTID in lstChanged: lstChanged.pop(row.OBJECTID) # raises error else: lstChanged.append(row.OBJECTID) class CreateLotNum(object): """Implementation for PyAddInWizard_addin.btn1 (Button)""" def __init__(self): self.enabled = True self.checked = False def onClick(self): lstDoubleCheck = [] for row in arcpy.SearchCursor(fc, '', '', field): lstDoubleCheck.append(row.OBJECTID) for item in lstChanged: if lstChanged(item) not in lstDoubleCheck: # raises error lstChanged.append(item) with open(r"C:\Temp\id.txt", "w") as f: # also for testing for item in lstChanged: f.write(lstChanged(item) + "\n")
for item in lstChanged: if item not in lstDoubleCheck: lstDoubleCheck.append(item) # not lstChanged
>>> aList = list() >>> aList.append(1) >>> >>> # not allowed: >>> aList(0) Traceback (most recent call last): File "<pyshell#4>", line 1, in <module> aList(0) TypeError: 'list' object is not callable >>> >>> # allowed: >>> aList[0] 1 >>> >>> # or: >>> for item in aList: print item 1 >>>
>>> lstChanged = [1,2,3] >>> lstDoubleCheck = [1,2] >>> >>> for item in lstChanged: if item not in lstDoubleCheck: # error fixed here lstChanged.append(item) # new error introduced here if len(lstChanged) == 20: # for demo purposes, forced a break break >>> # so here's what we have: >>> print lstChanged [1, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3] >>> # the integer item '3' would have been appended infinitely.
import arcpy import pythonaddins orderBySelOIDs = list() orderBySelOIDs.append('is it alive?') class globalListClass(object): def __init__(self): self.enabled = True print 'self aware?' def onEditorSelectionChanged(self): print 'onEditorSelectionChanged fired...' FIDreturn = arcpy.Describe(someLayer).FIDSet.split(';') if FIDreturn != ['']: selOIDs = [int(OID) for OID in FIDreturn] print 'currently selected oids: {0}'.format(str(selOIDs)) uniqueSel = set(selOIDs).difference(orderBySelOIDs) while len(uniqueSel) != 0: orderBySelOIDs.append(uniqueSel.pop()) print 'oid list in order selected: {0}'.format(str(orderBySelOIDs)) else: print 'no features in selection (or selection has been cleared)' print 'this executes also on starting Editor...' print 'what does orderBySelOIDs contain?: {0}'.format(str(orderBySelOIDs)) del orderBySelOIDs[:] print 'the ordered list has been reset (emptied): {0}'.format(str(orderBySelOIDs)) def openDocument(self): getLayers = arcpy.mapping.ListLayers(arcpy.mapping.MapDocument('current')) if len(getLayers) != 0: global someLayer someLayer = getLayers[0] print 'openDocument fired, setting global layer: {0}'.format(someLayer.name)
>>> openDocument fired, setting global layer: clip500 >>> onEditorSelectionChanged fired... no features in selection (or selection has been cleared) this executes also on starting Editor... what does orderBySelOIDs contain?: ['is it alive?'] the ordered list has been reset (emptied): [] >>> onEditorSelectionChanged fired... currently selected oids: [3] oid list in order selected: [3] >>> onEditorSelectionChanged fired... currently selected oids: [1] oid list in order selected: [3, 1] >>> onEditorSelectionChanged fired... currently selected oids: [4] oid list in order selected: [3, 1, 4] >>> onEditorSelectionChanged fired... currently selected oids: [2] oid list in order selected: [3, 1, 4, 2] >>> onEditorSelectionChanged fired... no features in selection (or selection has been cleared) this executes also on starting Editor... what does orderBySelOIDs contain?: [3, 1, 4, 2] the ordered list has been reset (emptied): []