Make Unique Alphanumeric value

321
15
Jump to solution
10-07-2020 02:19 AM
Highlighted
New Contributor III

Dear All,

            I want to calculate one field from another field e.g I want AAA- [FieldName], I want that AA will be change and give unique value e.g

AAA-[FieldName]

AAB-[FieldName]

ACA-[FieldName]

.....

etc

Thanks and regards

Bilal Alam

Reply
0 Kudos
1 Solution

Accepted Solutions
Highlighted
MVP Esteemed Contributor

The following code increments the first letter as well:

import arcpy
from itertools import product
from string import ascii_uppercase

lyr = # layer name or path to feature class
flds = ["Field_1", "Unique"]

incr = ("".join(i) for i in product(ascii_uppercase, ascii_uppercase, ascii_uppercase))

with arcpy.da.UpdateCursor(lyr, flds) as cur:
for fld1, uniq in cur:
cur.updateRow([fld1, "{}-{}".format(next(incr), fld1)])

View solution in original post

15 Replies
Highlighted
MVP Esteemed Contributor

Do you want the prefix to follow a pattern?  I can't see what the pattern would be from the examples (AAA-, AAB-, ACA-) you provide.  If you do want a pattern, is having the pattern applied based on ObjectID order sufficient or is there another order of the data you want the pattern applied to?

Reply
0 Kudos
Highlighted
New Contributor III

Actually I don't want any pattern. Only first letter will be constant e.g A and other will be random like AA or AB or CA etc.

Reply
0 Kudos
Highlighted
MVP Esteemed Contributor

You want unique values, but the first value will be A, which leaves only the 2nd and 3rd spots to change.  Assuming your values are case insensitive, e.g., AAb is the same as AAB, you will run out of unique values after 676 records.  Do you have less than 676 records?

Reply
0 Kudos
Highlighted
New Contributor III

Yes I have less then 676 records

Reply
0 Kudos
Highlighted
MVP Esteemed Contributor

OK.  Let us know if you need any more than what Bing Zheng provided.

Reply
0 Kudos
Highlighted
Esri Contributor

Hi Bilal

Hopefully below code could provide help.

abc = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

for i in range(0,26):
    for j in range(0,26):
        print ('A{0}{1}-[FieldName]'.format(abc,abc))

Regards,

Bing

Highlighted
New Contributor III

Dear Bing

Thanks for your help this work well when I run simply but I need to use this in arcpy.UpdateCursor and in place of [FieldName]. I want to use the values from one of the field already available in data. 

Regards 

Bilal

Reply
0 Kudos
Highlighted
Esri Contributor

Hi Bilal

Actually, if you want to use this list during the update process, I would like to suggest to put it in the dictionary.

for example.

abc = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

ind1 = 0

ind2 = 0

preStr= {}

for i in range(0,26):
    for j in range(0,26):

         preStr[ind] = 'A{0}{1}'.format(abc,abc)

         ind1 = ind1 + 1

fc = "c:/data/base.gdb/roads"

field1 = "field1"

field2 = "field2"

cursor = arcpy.UpdateCursor(fc)

for row in cursor:

      row.setValue(field2, preStr[ind2] + row.getValue(field1) * 3.0)

      ind2 = ind2 + 1

      cursor.updateRow(row)

cursor.updateRow(row)

Hopefully, it works for you.

Regards,

Bing

Reply
0 Kudos
Highlighted
New Contributor III

Dear Joshua and Bing,

Thanks for your kind help

I attach one picture which make you easy to understand.

In this picture fIeld _1 have value which have duplicate and I want to make unique code e.g AAC-4091

Reply
0 Kudos