# Assigning IDs to villages which may have duplicate names?

I have more than 150 village points, each with a village name attribute `village`. The same village name may appear on multiple points.

I want to assign individual IDs for each village name, say `vill_A = 1`, `vill_B = 2`, and so on. I tried to calculate `[village]`, but it didn't work.

Any suggestions?

• Are you wanting to assign a number to a village based on alphabetical order? Or do you just want a unique number for each village regardless of village name order? – Baltok Jan 14 '13 at 15:26
• Thanks Baltok, I don't care about alphabetical order, just unique numbers will do ..., though alphabetical order would be more helpful to keep track of things you know ... – ToNoY Jan 14 '13 at 15:41

The best way to handle the unique values where there can be duplicates is to use a Python dictionary to link the Village name to the id number.

Here is an example Python field calculation code block:

``````villages = {}
id = 1
def GetID(village):
global villages
global id
if not village in villages:
villages[village] = id
id += 1
return villages[village]
``````

Then simply call `GetID(!VillageNameField!)` for the expression.

This will create an empty Python dictionary called `villages`. Then, for each record in the Village Name Field it will remember if it has already assigned an id number to that village based on whether it already exists in the dictionary. If the village does not already exist in the dictionary it will assign it the next sequential id number. Finally, it returns the id number associated with the village from the dictionary whether it was already there or had to be added.

I agree with @Brad Nesom that calculating a field based on FID or OBJECTID is the simplest way, although if for some reason you need a python approach in the field calculator, try this calculation on a new integer field:

``````rec=0
def autoIncrement():
global rec
pStart = 1
pInterval = 1
if (rec == 0):
rec = pStart
else:
rec += pInterval
return rec
``````

Add to the box below the code block:

``````autoIncrement()
``````

Press OK

Probably the quickest way is,

1. create a new field for the values. i.e. [vill_id]
2. cacluate the oid (FID) into the vill_id.

• the same village name appears many times, therefore the village has many FIDs; I just found the following code, but it doesn't work: Convert String to Short Integer: MyInt = CInt( ["field name"] ) or CLng, CDbl "field name"= MyInt – ToNoY Jan 14 '13 at 15:57
• Create a summary table of all the possible village names, then run Brad's script on that table. Then you could do a join with the original data and copy that uniqueID over. – kenbuja Jan 14 '13 at 16:44
• @ToNoY that would have been useful information to include in your question. – blah238 Jan 14 '13 at 16:53
• Simple solution is to combine the answers from @kenbuja (summary statistics on village fieldname) and BradNesom (add/calculate new field equal to OID/FID) then join field by village fieldname to the original attribute table. – ccn Jan 14 '13 at 21:37

If you are looking for any programming solution, you may think about creating a dictionary. `IDictionary<string, List<int>> dictionary = new Dictionary<string, List<int>>();`

Here,

• Key = Village_name
• Value = List of oid

Now you can assign values for all these oids or whatever you want.