I've been looking extensively to find a solution for what I think is a very simple exercise.
In general, I would like to know how I can extract a variable from a loop in Python and then use it elsewhere in the script. I've limited Python knowledge, however I know from
R that this should be fairly simple. More specifically, my case is explained below.
I've two loops both multiplying rasters. I need to extract variables from each of the loops and then add them together.
import arcpy, os from arcpy import env from arcpy.sa import * arcpy.CheckOutExtension("spatial") arcpy.env.overwriteOutput = True env.workspace = "D:/Join" # outputFolder = env.workspace # # A list of two different rasters wheat = ["a_wheat", "b_wheat"] for w in wheat: r_w=Times("landuse_w",w) # landuse is a raster r_w.save(os.path.join(outputFolder, "r_" + w )) # This will generate r_a_wheat, r_b_wheat barl = ["a_barl", "b_barl"] for b in barl: r_b=Times("landuse_b",b)# landuse is a raster r_b.save(os.path.join(outputFolder, "r_" + b)) # This will generate r_a_barl, r_b_barl # # Add rasters add_a = r_a_wheat + r_a_barl add_b = r_b_wheat + r_b_barl
The problem with the loops like this is that I need to save everything and then add up. I would prefer to extract from
r_w the two rasters (
r_a_wheat, r_b_wheat), do the same for
r_b and then add up without having to save files. Is that possible?