mercredi 25 février 2015

Using lists in Python functions [on hold]


I have some code in a function that uses a list, which works, but is there a way to write a list outside of the function and use it within the function code?


For example in my code below I have a list called variables and then each type in variables is again listed in the WOTable with the code remap['type'] but I would like a code that does not relist the variable again with remap[type] in WOTable rather have a list outside the function where the list parameters can change but the function code stay the same. This script is for a weighted overlay in ArcGIS 10.2.


I have tried calling the list as weighted_overlay(variables, other parameters), and using just variables = [] in the function code, which did not work.



def weighted_overlay(raster1, raster2, raster3, out_raster):
in_raster1 = arcpy.Raster(raster1)
in_raster2 = arcpy.Raster(raster2)
in_raster3 = arcpy.Raster(raster3)

variables = ['remap1', 'remap2', 'remap3']
remap = {}
for type in variables:
remap[type] = RemapValue([[1, 1], [2, 2], [3, 3], [4,4], ["NODATA", "NODATA"]])

WOTable = WOTable([[in_raster1, 50, "VALUE", remap['remap1']],
[in_raster2, 35, "VALUE", remap['remap2']],
[in_raster3, 15, "VALUE", remap['remap3']]], [1, 9, 1])

outWeightedOverlay = WeightedOverlay(WOTable)
outWeightedOverlay.save(out_raster)
return


Sorry if this has been asked before but I was unable to find a solution and I do not have a lot of experience with python lists. Eventually I would like to make this a class function as part of a larger analysis script to reuse.





Aucun commentaire:

Enregistrer un commentaire