How to combine while and if for iteration? I want to calculated sum of the 'Inverse_DIST' of same 'IN_FID'

I use the arcpy.da.updatecursor of python code as follows

import arcpy
arcpy.env.workspace = "D:\Auto_Logit\Neartable.gdb"
nc = max(row for row in arcpy.da.SearchCursor("D:\Auto_Logit\Neartable.gdb\SP_15km_sewage", ['IN_FID']))

i = 0
j = 1
sum = 0

with arcpy.da.UpdateCursor("SP_15km_sewage",("OBJECTID","IN_FID","Inverse_DIST","Sum_Inverse_DIST")) as cursor :
for row in cursor :
while row == j and row == i :
j = j + 1
sum += row
if row == i :
row = sum
elif row != i :
i = i + 1
cursor.updateRow(row)

As a result, as follows my python code not working iteration

How to doing iteration working from my code??

• In what way is your code not working? Try using some print statements to see what is being run and what is not. – PolyGeo Aug 18 '16 at 7:09

I would use another approach, without while loop but iterating over the unique values for which you want to calculate a sum:

import arcpy

fc = r"D:\Auto_Logit\Neartable.gdb\SP_15km_sewage"

## first get a list of unique IN_FID values
fid_list = sorted({row for row in arcpy.da.SearchCursor(fc, "IN_FID")})

## then iterate over these IN_FID values: for each IN_FID,
## calculate the sum of the Inverse_DIST field values and add it to the sum field.
for fid in fid_list:
exp = """IN_FID = '{}'""".format(fid)
sum = 0
with arcpy.da.SearchCursor(fc, "Inverse_DIST", exp) as cur_sum:
for row_sum in cur_sum:
sum += row_sum

with arcpy.da.UpdateCursor(fc, "Sum_Inverse_DIST", exp) as cur:
for row in cur:
row = sum
cur.updateRow(row)
• Thank you for your advice and help, it is very contribution in my research study. Expressing gratitude once again. – Kimgoon Aug 18 '16 at 11:28
• You're welcome :) Glad I could help. – GISGe Aug 18 '16 at 11:30

Try this code:

import arcpy
input = r"C:\....\test\New File Geodatabase.gdb\line"
output = r"C:\....\test\New File Geodatabase.gdb\line_statistic"
arcpy.Statistics_analysis(input, output, "Shape_Leng SUM", "Code")

Shape_Leng - is your Invers_Dist SUM - is the code for summarize Code - is you IN_FID

is sure to work couse under Analysis Tools\Statistics\Summery, is a tool to make this problem without coding.

• Florjan - when adding a code block use the {} button to format as code (or just indent each line by 4 spaces) – Midavalo Aug 18 '16 at 8:32
• Thanks for your advice. You who have provided your are very simple. it is a good imformation for my future research – Kimgoon Aug 18 '16 at 11:31