Make Unique Alphanumeric value

2558
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
15 Replies
JoshuaBixby
MVP Esteemed Contributor

Can you elaborate on

I want to use the values from one of the field already available in data. 

Do you have an existing field that is partially populated already?  If so, do you want to use those values if they already exist but add new ones if they are missing?  And, do you want to make sure any new values are not the same as existing values?

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
JoshuaBixby
MVP Esteemed Contributor

Try this in interactive Python Window, just update the value for lyr.

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))

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

Bmohammad
New Contributor III

Thanks Joshua it works thanks for your kind help

It stops when it works up to AZZ-222 if we need more then we have to change 

 cur.updateRow([fld1, "B{}-{}".format(next(incr), fld1)])

first letter to B

Is there any method that when A finished it automatically change A to B and so on

Thanks Joshua

0 Kudos
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)])
Bmohammad
New Contributor III

Thanks Brother it works. I sincerely appreciate your help

0 Kudos