2

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.

2
  • 2
    Define your list out side the function, and make a parameter argument of the function.
    – artwork21
    Feb 25, 2015 at 13:55
  • It may be best to set the variables list in the global scope. Since you say the parameters of the list can change in other parts of the code, it may be best to copy the list for uses in other functions because they are mutable and any changes to that list in the code will be reflected elsewhere. You can create local copies inside functions by using variables[:].
    – crmackey
    Feb 25, 2015 at 14:38

2 Answers 2

1

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

If you are defining the list outside of the function, you do not want to then redefine it within the function.

def weighted_overlay(variables, other parameters):
    variables = []
    print variables

variables = ['remap1', 'remap2', 'remap3']
weighted_overlay(variables, other parameters)

This outputs an empty list [], since variables = [] inside the function changed its contents.

def weighted_overlay(variables, other parameters):
    print variables

variables = ['remap1', 'remap2', 'remap3']
weighted_overlay(variables, other parameters)

This outputs ['remap1', 'remap2', 'remap3'] -- the function knows what's in the list, because you passed the list to the function.

def weighted_overlay(variables, other parameters):
    print variables

weighted_overlay(['remap4', 'remap5', 'remap6'], other parameters)

You can put the variables directly inside the function call if you want to save on code space. This also outputs ['remap1', 'remap2', 'remap3'].


Since each of the variables entries ends up referring to the same RemapValue, I'd instead suggest still using a list, but in a different way:

value = [50, 35, 15]

def weighted_overlay(value, other parameters):
    #other code
    remap = RemapValue([[1, 1], [2, 2], [3, 3], [4,4], ["NODATA", "NODATA"]])
    WOTable = WOTable([[in_raster1, value[0], "VALUE", remap],
                       [in_raster2, value[1], "VALUE", remap],
                       [in_raster3, value[2], "VALUE", remap]], [1, 9, 1])
    #other code
2
  • This worked for variables as did the global variables. This might be off topic but can I also include a second list such as a list for [50, 35, 15] using the same method and have it work in WOTable?
    – marion
    Feb 25, 2015 at 21:30
  • In short, yes; looking at your code in more detail, you might want to adjust the approach (see edit above in just a moment)
    – Erica
    Feb 25, 2015 at 21:41
0

This question is a pure python question. In addition to @artwork21 suggestion, you can make your list a global variable and then manipulate that variable in any function. Please see Using global variables in a function other than the one that created them

2
  • I did try that using global type but I wasn't sure how to write it in the WOTable. if I use remap[type] the code doesn't read the list in variables if I use just remap I get an unending loop.
    – marion
    Feb 25, 2015 at 14:33
  • Using global variables sets variables to be accessible to any function. You don't need to then refer to it in any special way -- just keep the code as is otherwise :)
    – Erica
    Feb 25, 2015 at 14:46

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