Can someone please tell me what's happening here. I'm still new to Python for ArcGIS and I really need to understand this piece of code for my project.
How does dct_max
work and how is tpl
created?
At first dct_max
seems to me like an empty dictionary. Later its changed to a list?. What does this line mean - if from_g in dct_max:
. I can't find out what in
does. Also the line tpl = dct_max[from_g]
seems to create a list called tpl
with a single item. Then how can there be any meaning to tpl[1]
???
def main():
import arcpy
arcpy.env.overwriteOutput = True
# input fc
fc = r"C:\Forum\Gemeinde\NRW_Gemeinde_SpatiaJoin.shp"
fld_from = "GEN"
fld_to = "GEN_1"
fld_val = "Value"
# output field (will be added to input fc)
fld_out = "Gemeindes"
# output fc
fc_out = r"C:\Forum\Gemeinde\NRW_Gemeinde_diss01.shp"
# detemine the max commutors per from gemeinde
dct_max = {}
with arcpy.da.SearchCursor(fc, (fld_from, fld_to, fld_val)) as curs:
for row in curs:
from_g = row[0]
to_g = row[1]
val = row[2]
# check that from_Gemeinde is not equal to to_Gemeinde
if from_g != to_g:
if from_g in dct_max:
# check if value is higher
tpl = dct_max[from_g]
max_to_g = tpl[0]
max_val = tpl[1]
if val > max_val:
# update entry in dct
dct_max[from_g] = (to_g, val)
else:
# insert value
dct_max[from_g] = (to_g, val)