Select to view content in your preferred language

sequential number for each class in a field

1851
1
08-27-2013 03:15 AM
nadeemfareed
Deactivated User
hello everyone.
i know a script which is used for sequential number generation in a field. the script is given below.
Expression:
autoIncrement()

Expression Type:
PYTHON_9.3

Code Block:
rec=0
def autoIncrement():
global rec
pStart = 1 #adjust start value, if req'd
pInterval = 1 #adjust interval value, if req'd
if (rec == 0):
  rec = pStart
else:
  rec = rec + pInterval
return rec


this is very useful when we want to calculate the sequence number for whole field.
but i am looking for same thing in a different manner. i have point shapefile which have different classes, each class have more then 100 records. i want to generate the sequence number for each class in a single field. e.g..
class1= 101 records, class2=105 records, class3=110 records.these records are present in classes field
i have another field name seq.numer. in this field i want to generate sequential number for each class in this field starting from 1 for each class. i hope you understand what i mean.
Nadeem Fareed
Tags (2)
0 Kudos
1 Reply
JakeSkinner
Esri Esteemed Contributor
Hi Nadeem,

If you don't have to use the field calculator, the below should work:

import arcpy
from arcpy import env
env.workspace = r"C:\temp\python\test.gdb"

list = []
rows = arcpy.SearchCursor("Table")
for row in rows:
    Class = row.getValue("Class")
    list.append(Class)

del row, rows

#remove duplicates from list
list = dict.fromkeys(list)
list = list.keys()

for n in list:
        rec = 0
        rows = arcpy.UpdateCursor("Table")
        for row in rows:
            if row.Class == n:                            
              pInterval = 1
              if (rec == 0):
                rec = 1
                row.SeqNumber = rec
              else:
                rec = rec + pInterval
                row.SeqNumber = rec
            rows.updateRow(row)

del row, rows
0 Kudos