0

This question already has an answer here:

I am having a problem with adding sequential numbers to the new column based on other sorted value. I would like to use 'Sort by' column to be able to generate numbers in the 'Required number'column.

I have a script listed below (Field calculator) that allows me to generate numbers in 'Script used' column. Unfortunately, the numbers start from 0 instead of 11.

mxd = arcpy.mapping.MapDocument("CURRENT")
lr=arcpy.mapping.ListLayers(mxd)[0]

tbl=arcpy.da.TableToNumPyArray(lr,("objectid","MapOrderNum"))#MapOrderNum is a field that you want to use to sorb by
bs=sorted(tbl,key=lambda x: x[1])
aDict={}
for i,row in enumerate(bs):
 aDict[row[0]]=i
def sortSeq(objectid):
 return aDict[objectid]

sortSeq ( !OBJECTID!)

enter image description here

marked as duplicate by FelixIP, whyzar, ahmadhanb, Jochen Schwarze, jbalk Jan 23 at 21:47

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    Try to use for i,row in enumerate(bs, 11) instead of for i,row in enumerate(bs) – ahmadhanb Jan 23 at 11:48
  • Or return aDict[objectid]+11. You can use da.SearchCursor instead of TableToNumPyArray, not sure what will be faster: tbl = [i for i in arcpy.da.SearchCursor(lr, ["OID@","MapOrderNum"])]. Will be a list of tuples which you can sort the same way. – BERA Jan 23 at 12:22