import arcpy
# fc = "D:/Xander/tmp/NIData.gdb/test_copy"
fc = "C:/Temp/lorraine/NIData.gdb/fc_f6000_in99Matx"
fld_id = "RegID_6k99"
fld_label = "FLABEL"
fld_flows = "RegIntFlows"
# create a unique list of all the FLABEL values
lst_labels = list(set([row[0] for row in arcpy.da.SearchCursor(fc, (fld_label))]))
# Create the list of fields to be used in the SearchCursor
flds = [fld_id, fld_label]
flds.extend(lst_labels)
# create list of all fields
flds_all = [fld.name for fld in arcpy.ListFields(fc)]
# make sure that the relevant fields for the SearchCursor exist in fc
flds = list(set(flds) & set(flds_all))
# create a dictionary with for each RegID_6k99 a list of corresponding FLABEL fields
dct_ref ={}
flds2 = (fld_id, fld_label)
with arcpy.da.SearchCursor(fc, flds2) as curs:
for row in curs:
uni_id = row[0]
lbl = row[1]
if uni_id in dct_ref:
lst_lbls = dct_ref[uni_id]
if not lbl in lst_lbls:
lst_lbls.append(lbl)
else:
lst_lbls = [lbl]
dct_ref[uni_id] = lst_lbls
# loop through data and fill the result dictionary
flds.append(fld_flows)
with arcpy.da.UpdateCursor(fc, flds) as curs:
for row in curs:
uni_id = row[flds.index(fld_id)]
if uni_id in dct_ref:
lst_lbls = dct_ref[uni_id]
val = 0
for lbl in lst_lbls:
if lbl in flds:
val += row[flds.index(lbl)]
else:
print " - lbl {0} in field FLABEL for RegID_6k99 = {1} does not exist as field".format(lbl, uni_id)
row[flds.index(fld_flows)] = val
curs.updateRow(row)
else:
print "key uni_id {0} not found in dct_ref".format(uni_id)