rows = arcpy.UpdateCursor("Junk1.dbf") fields = arcpy.ListFields("Junk1.dbf" ) #Create an empty list myList = [] i = 0 for row in rows: for field in fields: if field.name == 'ID': value = row.getValue(field.name) if value in myList: i += 1 row.TEST = i rows.updateRow(row) if value not in myList: myList.append(value)
This is has been extremely helpful. Quick question as I am new to arcpy. If I want to mark ALL repeated IDs with a flag and not necessarily just the second, third, fourth occurrence, how might I accomplish this? For instance, if I had the following list of values:abcdeeWith the above scripts, I would geta-0b-0c-0d-0e-0e-1What I want is a-0b-0c-0d-0e-1e-1 or 2 (Doesn't matter as long as I can see that this value is not equal to zero).My goal is to flag the values that are duplicates, identify the value and then assign a new value to those records. If I split a polygon, for instance, I don't want to maintain the ID of the old one, I want to seek out the value and replace it with two new unique IDs.Thanks in advance.
rec=0 def autoIncrement(): global rec pStart = 1 #adjust start value to be the next number in the ID series pInterval = 1 #adjust interval value, if req'd if (rec == 0): rec = pStart else: rec = rec + pInterval return rec
# Import the arcpy module import arcpy pStart = 1 # adjust start value, if required pInterval = 1 # adjust interval value, if required # Initialize the sequence number dictionary and the Route ID variables seqDict = {} ID = -1 highest = 0 changed = False # Assign data and field list variables. # Customize these variable inputs for your specific data myData = r"C:\MyPath\MyData.shp" fields = ["ID"] # Step 1 - Use an search cursor to get counts of the unique ID numbers rows = arcpy.da.SearchCursor(myData, fields) for row in rows: if row[0] is None: ID = "Null" else: ID = row[0] if ID in seqDict: seqDict[ID] = seqDict[ID] + pInterval elif ID = 'Null': seqDict[ID] = 2 else: seqDict[ID] = pStart if highest < ID: highest = ID del row del rows # Step 2 - Use an update cursor to update all records with a count greater than 1 rows = arcpy.da.UpdateCursor(myData, fields) for row in rows: if row[0] is None: ID = "Null" else: ID = row[0] if seqDict[ID] > 1: highest = highest + pInterval row[0] = highest changed = True if changed: rows.updateRow(row) changed = False del row del rows
any ideas on getting this run in the field calculator?
rows = arcpy.UpdateCursor("Junk1.dbf","","","","ID A") # sort by ID, ascending order #Create an empty list myList = [] i = -1 for row in rows: if i == -1: #first time around value = row.ID myList.append(value) i += 1 print value, i if row.ID != value: #if a new ID value = row.ID myList.append(value) i = 0 row.TEST = i rows.updateRow(row)
rows = arcpy.UpdateCursor("Junk1.dbf","","","","ID A") # sort by ID, ascending order #Create an empty list myList = [] i = 0 for row in rows: if i == 0: #first time around value = row.ID myList.append(value) i += 1 print value, i if row.ID != value: #if a new ID value = row.ID myList.append(value) i = 1 row.TEST = i - 1 rows.updateRow(row)
I usually use the approach of sorting by values, then comparing the previous value to the current value: rows = arcpy.UpdateCursor("Junk1.dbf","","","","ID A") # sort by ID, ascending order #Create an empty list myList = [] i = 0 for row in rows: if i = 0: #first time around value = row.ID myList.append(value) i += 1 if row.ID != value: #if a new ID value = row.ID myList.append(value) i = 1 row.TEST = i rows.updateRow(row)
rows = arcpy.UpdateCursor("Junk1.dbf","","","","ID A") # sort by ID, ascending order #Create an empty list myList = [] i = 0 for row in rows: if i = 0: #first time around value = row.ID myList.append(value) i += 1 if row.ID != value: #if a new ID value = row.ID myList.append(value) i = 1 row.TEST = i rows.updateRow(row)
import arcpy rows = arcpy.UpdateCursor("Filename") mylistarray = [] found = False for row in rows: value = row.getValue("ID") for x in mylistarray: if x[0] == value: found = True x[1] +=1 row.TEST = x[1] if not found: mylistarray.append([value,1]) row.TEST = 1 else: found = False rows.updateRow(row)
Angemeldete Mitglieder können Beiträge verfassen, Updates folgen und mehr. Neu hier? Registriere ein kostenloses Konto.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.