Select to view content in your preferred language

increment duplicate records based on a field

1106
4
09-19-2012 07:59 AM
NancyMarth
Deactivated User
Hi,

Looking for some simple code to run either in Field Calculator (preferred) or in a python script on a table to create a field and add incremental numbers to that field based on repeating values of another field, for all records.  I've already permanently sorted the table, which I need to do before incrementing the duplicate records.  I can create the field no problem to minimize steps necessary, if someone can provide the basic code for the increment based on a field.  Thanks.

Example table:
FieldA     Dups (created)
3            1
3            2
3            3
1            1
1            2
1            3
1            4
8            1
8            2
Tags (2)
0 Kudos
4 Replies
JakeSkinner
Esri Esteemed Contributor
Hi Nancy,

Try the following in the field calculator:

d = {}

def findDuplicates(inVal):
  try:
    d[inVal] += 1 
    return d[inVal] 
  except KeyError:
    d[inVal] = 1
    return 1


findDuplicates(!FieldA!)


Be sure 'Python' is checked within the Field Calculator.
0 Kudos
AlessandroCinnirella
Regular Contributor
i made it too quickly. test it!

a = [3,3,3,1,1,2,2,5,6,8,8] #the input values
b = [] #temp list
c = [] #duplicate value list

for i in a:
    if len(b)< 1:
        b.append(i)
        c.append(len(b))        
    else:        
        if i in b:
            b.append(i)
            c.append(len(b))            
        else:           
            b = []
            b.append(i)
            c.append(len(b))



hope this helps.

ciao,
AC
0 Kudos
markdenil
Frequent Contributor
I think you needn't have sorted the table.
If you build a list of unique values for FieldA
and then itterate through the list to
make a layer (or table view), in turn, of the records with that value as the FieldA value

Run an update query on the layer or view
incrementing a counter and assigning the counter value to Dup field
Then blow that layer away, and make a layer of the next FieldA value in the list.

for each new layer, reinitalize the counter, so the first instance is 1 (or 0).


EDIT -> 3 simultaneous replies! HA!
0 Kudos
NancyMarth
Deactivated User
Jake,

This works perfectly.  Thanks very much.
0 Kudos