Select to view content in your preferred language

Create Sequential Numeric ID in Groups

975
4
04-11-2011 11:47 AM
ScottReaves
Deactivated User
I have a set of records:
OID,Type
1,A
2,B
3,A
4,B
5,C
6,C
7,B
8,B
9,A

I'd like to use a python script to add a sequential unique ID by type...
OID,Type,Type_Num
1,A,1
2,B,1
3,A,2
4,B,2
5,C,1
6,C,2
7,B,3
8,B,4
9,A,3

So far i've attempted looping through a list of types but with only partial success and i'm convinced i'm way off base as evidenced by code below...
rec=0
L=["A","B","C"]
def doit (Type) :
  for name in L :
    while Type == name :
      global rec
      rec = rec + 1
      return rec
    rec=0
__esri_field_calculator_splitter__
doit (!Type!)

My ultimate goal is to reference my records in the fashion A1, A2, A3, B1, B2, B3 etc.  Any thoughts or suggestions would be much appreciated.  Obviously i'm VERY new to Python.
Tags (2)
0 Kudos
4 Replies
DarrenWiens2
MVP Honored Contributor
Here's a standalone script that should do what you want (just change the path to your feature class):
import arcpy

input = "H:/GIS_Data/TEMP_GDB.gdb/feattoline" # your feature class
fields = "OID A; Type A" # sort by OID, ascending, then Type, ascending

rows = arcpy.UpdateCursor(input,"","","",fields)

count = 0
for row in rows:
    if count == 0:
        curType = row.Type
    count = count + 1
    if row.Type != curType:
        curType = row.Type
        count = 1
    row.Type_Num = count
    rows.updateRow(row)

del row
del rows
0 Kudos
TerrySilveus
Frequent Contributor
I have a set of records:
OID,Type
1,A
2,B
3,A
4,B
5,C
6,C
7,B
8,B
9,A

I'd like to use a python script to add a sequential unique ID by type...
OID,Type,Type_Num
1,A,1
2,B,1
3,A,2
4,B,2
5,C,1
6,C,2
7,B,3
8,B,4
9,A,3

So far i've attempted looping through a list of types but with only partial success and i'm convinced i'm way off base as evidenced by code below...
rec=0
L=["A","B","C"]
def doit (Type) :
  for name in L :
    while Type == name :
      global rec
      rec = rec + 1
      return rec
    rec=0
__esri_field_calculator_splitter__
doit (!Type!)

My ultimate goal is to reference my records in the fashion A1, A2, A3, B1, B2, B3 etc.  Any thoughts or suggestions would be much appreciated.  Obviously i'm VERY new to Python.


look at this link... a very similar question has been answered.
http://forums.arcgis.com/threads/27926-Find-Duplicates-and-increment
0 Kudos
MathewCoyle
Honored Contributor
I have something like this.

import arcpy
Acount = 0
Bcount = 0
Ccount = 0
rows = arcpy.UpdateCursor("mydata", "", "", "OID; Type; Type_Num", "OID A")

for row in rows:
    if row.Type == 'A':
        Acount = Acount+1
        row.Type_Num = Acount
    elif row.Type == 'B':
        Bcount = Bcount+1
        row.Type_Num = Bcount
    elif row.Type == 'C':
        Ccount = Ccount+1
        row.Type_Num = Ccount
    rows.updateRow(row)
del row
del rows
0 Kudos
ScottReaves
Deactivated User
Thank you both, i believe this will get me there!
0 Kudos