Sunday 30 June 2019

arcgis desktop - Python script for identifying duplicate records (follow up)


I am trying to calculate a field that marks duplicates and sequentially numbers them. I am using the code below that I found here: Generating sequential numbering for duplicate values in a field sorted by ObjectID.


It works, but I get a duplicate starting value before it begins sequentially numbering unless I only run the field calculator on the selected values. When I calculate after manually selecting the duplicate rows and running it on only those values, it works fine.


Here's the code:


prevFieldValue = ''
counter = 0
def GetDuplicateCounter(myFieldValue):
global prevFieldValue
global counter

if myFieldValue == prevFieldValue:
counter += 1
else:
counter = 1
prevFieldValue = myFieldValue
return counter

Results running it on all roads (nothing selected):


calculated without selection


Results running it after manually selecting the duplicated fields (not too efficient!).



calculated on selected records



Answer



Here's how I solved the problem:


Pre-Logic Script Code:
d = {} # creates an empty dictionary the first time

def find_duplicates(val):
d[val] = d.setdefault(val, -1) + 1
return d[val]


Expression: dup =
find_duplicates(!Unique_ID!)

Unfortunately, I can't remember where I found the answer but if I do, I will post the credit.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...