Python: retrieving from list & adding autoincrement IDs

2802
14
07-14-2014 08:27 AM
HayleySmall
New Contributor II

Hi all,

I was wondering if you could help me write a script for Field Calculator.

I have a list of 2,000+ entries contained in about 40 parks. Each park has a field ParkID which is already filled out. I'd like to create a script that generates UniqueIDs based on the ParkIDs.

Ex. ParkID = Q001

.:. UniqueIDs = Q001_001, Q001_002, all the way to Q001_100 or however many necessary depending on how many entries, but then beginning at 001 again when reaching a new park, i.e. R142_001, R142_002, etc.

I want to first retrieve each ParkID from the list of ParkIDs and then use that list to create an autoincrement starting at 001 for each unique ParkID.

I'd rather do this that just do an autoincrement for each park individually.

I have very little python experience and might not quite understand some details, so could you please explain in depth?


EDIT:
Right now I have the following script in the Codeback of Field Calculator:

rec=0

plot = ""

def autoIncrement(park):

global rec

global plot

pStart = 1 

pInterval = 1

if (rec == 0): 

  rec = pStart 

  plot = park

  plot = plot + "_000"+ str(rec)

  rec += pInterval

elif (rec < 10): 

   plot = park + "_000" + str(rec)

   rec += pInterval  

elif (rec > 9 and rec < 100): 

   plot = park + "_00" + str(rec)

   rec += pInterval 

elif (rec > 99 and rec < 1000): 

  plot = park + "_0" + str(rec)

  rec += pInterval

else:

   plot = park +  "_" + str(rec)

   rec += pInterval 

return plot

And the following in the "FIELD_NAME" = autoIncremet(!ParkID!)

This code works, but starts the unique ID at 0 and goes up into the thousands. I am trying to find a way to make it so for each new ParkID, the unique ID restarts at 001 again.

Best,

Hayley

Message was edited by: Hayley Small

14 Replies
ChristopherMoravec1
New Contributor II

Hayley,

I know you got it working, but I went ahead and took a pass at a script anyway.  Basically I created a dictionary that holds the "Park ID" values where the Park ID is the key, and stored the number of records that we have encountered for that park as the value.  What this allows the code to do is keep track of how many times it has encountered a particular Park ID and use that number to create the new unique ID.  It is not smart enough to updated only null records, instead it will just rebuild all the Id's, that may not be what you want in the end.

Here is the code to put in the "Pre-Logic Script Code":

global id_value_dict

id_value_dict = dict()

def calc_new_id(base_id_value):

   global id_value_dict

   if base_id_value in id_value_dict:

      id_value_dict[base_id_value] += 1

   else:

      id_value_dict[base_id_value] = 1

   return str(base_id_value) + "_" + str(id_value_dict[base_id_value]).zfill(3)

You can also check out a gist of it here: A quick script to create a unique ID based on the value of a different column. For example, the colu...

Hope that helps!

Christopher

TomGoeke
New Contributor III

Thanks for this code.  I was looking for an auto-increment tool that would work on a varying base id field.  This worked perfect.

0 Kudos
ToddUlery
Occasional Contributor

Looks perfect. There is really no 'one' way to do this. I was going to suggest looking at that script and alter it. I ran my script on data that was more perfect and 'yes' it was ascending nicely. It was not accounting for hiccups; just a straight run.

If anything I am glad you were able to get it figured out enough to get through it. Again if there are 2000 + these records on multiple tables, I might suggest writing a separate python script.

Would be glad to help if you need it.

Take care,

Todd

0 Kudos
ToddUlery
Occasional Contributor

Hey Hayley, I am currently writing a tool you could run. It expands on Christopher's input for using Dictionaries. Brilliant input. You may have already worked this out, but I will complete it anyway.

0 Kudos
ToddUlery
Occasional Contributor

Here is a tool you can use Hayley.

It has 3 parameters:

  • Path for table/fc
  • ParkID Field
  • UniqueID Field

It can be batched if needed.

Please feel free to open it up and look at the guts of it, and don't hesitate to ask anything.

  • Will Need to export it; I have imported it.

Of course that goes for anybody else that wants to use it too.

Ex. of prints. Should see in geoproc.:

...Unique ID S0029_010 added for S

Value IS NULL, Empty String, OR UNKNOWN

Value IS NULL, Empty String, OR UNKNOWN

Value IS NULL, Empty String, OR UNKNOWN

Value IS NULL, Empty String, OR UNKNOWN

0034 ...Value IS GOOD

{'Q': 10, ' ': 1, 'S': 10, 'R': 1}

0034 

...Unique ID  0034_001 added for 

T0035 ...Value IS GOOD

{'Q': 10, ' ': 1, 'S': 10, 'R': 1, 'T': 1}

T0035 T

...Unique ID T0035_001 added for T

I am determined to make this work. So let me know if i have not accounted for something and share the error.

Thanks,

Todd

0 Kudos