I'd like to create a field of sequential numbers starting at 001 counting all the way until 322. Every number must be 3 digits. How do I do this?
Thank you all for your help.
Hi James,
You can do this using the Field Calculator. Here is an example:
rec<SPAN class="operator token">=</SPAN><SPAN class="number token">0</SPAN> <SPAN class="keyword token">def</SPAN> <SPAN class="token function">autoIncrement</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">global</SPAN> rec pStart <SPAN class="operator token">=</SPAN> <SPAN class="number token">1</SPAN> <SPAN class="comment token">#adjust start value, if req'd </SPAN> pInterval <SPAN class="operator token">=</SPAN> <SPAN class="number token">1</SPAN> <SPAN class="comment token">#adjust interval value, if req'd</SPAN> <SPAN class="keyword token">if</SPAN> <SPAN class="punctuation token">(</SPAN>rec <SPAN class="operator token">==</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> rec <SPAN class="operator token">=</SPAN> pStart <SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN> rec <SPAN class="operator token">=</SPAN> rec <SPAN class="operator token">+</SPAN> pInterval <SPAN class="keyword token">if</SPAN> len<SPAN class="punctuation token">(</SPAN>str<SPAN class="punctuation token">(</SPAN>rec<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">==</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">return</SPAN> <SPAN class="string token">"00"</SPAN> <SPAN class="operator token">+</SPAN> str<SPAN class="punctuation token">(</SPAN>rec<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">elif</SPAN> len<SPAN class="punctuation token">(</SPAN>str<SPAN class="punctuation token">(</SPAN>rec<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">==</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">return</SPAN> <SPAN class="string token">"0"</SPAN> <SPAN class="operator token">+</SPAN> str<SPAN class="punctuation token">(</SPAN>rec<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">elif</SPAN> len<SPAN class="punctuation token">(</SPAN>str<SPAN class="punctuation token">(</SPAN>rec<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">==</SPAN> <SPAN class="number token">3</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">return</SPAN> str<SPAN class="punctuation token">(</SPAN>rec<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></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
Thanks Jake. That's perfect!
Another approach, instead of using global variables, is to use a generator:
Code Block:
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">gen</SPAN><SPAN class="punctuation token">(</SPAN>x<SPAN class="operator token">=</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">while</SPAN> <SPAN class="token boolean">True</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">yield</SPAN> <SPAN class="string token">"{:0=3}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>x<SPAN class="punctuation token">)</SPAN> x<SPAN class="operator token">+=</SPAN><SPAN class="number token">1</SPAN> g <SPAN class="operator token">=</SPAN> gen<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>
Expression:
next<SPAN class="punctuation token">(</SPAN>g<SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN></SPAN>
Signed in members can post, follow updates, and more. New here? Register a free account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.