0

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)
1
  • 5
    I would suggest reading up on Python data structures (this section in the Python Tutorial is very good). Also, there isn't anything here GIS-specific, so StackOverflow would be a more appropriate venue.
    – blah238
    Commented Jun 8, 2015 at 20:36

1 Answer 1

6

in provides a quick logical test for determining if a value is contained in a list, dict, or string.

if from_g in dct_max: 

either from_g is found in dct_max (true) or it's not (false).

The rest of your questions would be resolved by researching how dict objects work (lots of good examples on Stack Overflow).

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.