I've been trying to find a way to call in field names from a zip(). Basically I have 12 fields called "Flw_days1", "Flw_days2" etc and corresponding "Depth" fields that need to be calculated using the inputs of the "Flw_days" fields. I.e. "Depth1" is calculated using "Flw_Days1" and so on.
I've used the zip() function to match these together and then tried to call these into a codeblock and expression for use in the field calculator.
See example below:
depth_fields = ["Depth1", "Depth2", "Depth3", "Depth4", "Depth5", "Depth6", "Depth7", "Depth8", "Depth9", "Depth10", "Depth11", "Depth12"]
flow_fields = ["!Flw_Day1!", "!Flw_Day2!", "!Flw_Day3!", "!Flw_Day4!", "!Flw_Day5!", "!Flw_Day6!", "!Flw_Day7!", "!Flw_Day8!", "!Flw_Day9!", "!Flw_Day10!", "!Flw_Day11!", "!Flw_Day12!"]
zipped = zip(sorted(depth_fields), sorted(flow_fields))
for input in zipped:
codeblock = """
def depth(flow, kfc, rf):
if kfc == 0 or rf == 0:
return ""
else:
return (flow * (kfc / rf) + 2)
"""
expression = "depth(input[0], !K_FC!, !Ret_Fact!)"
arcpy.CalculateField_management(CMPP_fc, input[1], expression, "PYTHON_9.3", codeblock)
As you can see I have tried to call the "Flw_Days" field into the expression and the "Depth" field into the field calculator.
The error I get is:
Runtime error <class 'arcgisscripting.ExecuteError'>: ERROR 000539: Error running expression: depth(input[0], 0, 0) <type 'exceptions.TypeError'>: 'builtin_function_or_method' object is unsubscriptable Failed to execute (CalculateField).
So it seems to be calling the input[0] into the expression that is causing the issue.
I am using ArcGIS Desktop 10.
"!Depth1!"
?zip(sorted(flow_fields), sorted(depth_fields))
-- depth_fields should be first, flow_fields should be second.