Make Unique Alphanumeric value

2631
15
Jump to solution
10-07-2020 02:19 AM
Bmohammad
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

0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
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
JoshuaBixby
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?

0 Kudos
Bmohammad
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.

0 Kudos
JoshuaBixby
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?

0 Kudos
Bmohammad
New Contributor III

Yes I have less then 676 records

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

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

0 Kudos
by Anonymous User
Not applicable

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

Bmohammad
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

0 Kudos
by Anonymous User
Not applicable

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

0 Kudos
Bmohammad
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

0 Kudos