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
The following code increments the first letter as well:
<SPAN class="keyword token">import</SPAN> arcpy <SPAN class="keyword token">from</SPAN> itertools <SPAN class="keyword token">import</SPAN> product <SPAN class="keyword token">from</SPAN> string <SPAN class="keyword token">import</SPAN> ascii_uppercase lyr <SPAN class="operator token">=</SPAN> <SPAN class="comment token"># layer name or path to feature class</SPAN> flds <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token">"Field_1"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Unique"</SPAN><SPAN class="punctuation token">]</SPAN> incr <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="string token">""</SPAN><SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>i<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">for</SPAN> i <SPAN class="keyword token">in</SPAN> product<SPAN class="punctuation token">(</SPAN>ascii_uppercase<SPAN class="punctuation token">,</SPAN> ascii_uppercase<SPAN class="punctuation token">,</SPAN> ascii_uppercase<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">with</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>UpdateCursor<SPAN class="punctuation token">(</SPAN>lyr<SPAN class="punctuation token">,</SPAN> flds<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">as</SPAN> cur<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">for</SPAN> fld1<SPAN class="punctuation token">,</SPAN> uniq <SPAN class="keyword token">in</SPAN> cur<SPAN class="punctuation token">:</SPAN> cur<SPAN class="punctuation token">.</SPAN>updateRow<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">[</SPAN>fld1<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"{}-{}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>next<SPAN class="punctuation token">(</SPAN>incr<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> fld1<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
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
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?
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)
Hopefully, it works for you.
Regards,
Bing
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
Yes I have less then 676 records
Hopefully below code could provide help.
for i in range(0,26): for j in range(0,26): print ('A{0}{1}-[FieldName]'.format(abc,abc))
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?
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.
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?
Los miembros registrados pueden publicar, seguir actualizaciones y más. ¿Nuevo aquí? Regístrate gratis.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.