Use nested for loop to find all possible combinations of rasters in geodatabase using Python

arcpyfor looppythonraster

I have one geodatabase with multiple rasters where the raster name should automatically identify which category a raster belongs to. I want to combine SLR named rasters with SLOSH named rasters. I am not sure how to approach the nested loop and combine the values of combinations of rasters.

Current code:

import arcpy
from arcpy import env
from pathlib import * 
scriptDir = Path.cwd()
dbDir = scriptDir / 'Inundation_handout.gdb'
env.workspace = str(dbDir)
env.overwriteOutput = True

rasterList = arcpy.ListRasters()
for r1 in rasterList:
    myras = arcpy.Raster(r1)
    for r2 in rasterList:
        if 'SLOSH' in r2 and not 'ft' in r2:

Best Answer

Instead of a nested loop, try itertools.product.

# Completely untested...
from itertools import product
import arcpy
from arcpy import env
from pathlib import * 
scriptDir = Path.cwd()
dbDir = scriptDir / 'Inundation_handout.gdb'
env.workspace = str(dbDir)
env.overwriteOutput = True

sloshList = arcpy.ListRasters("*slosh*")
slrList = arcpy.ListRasters("*slr*")
for r1, r2 in product(sloshList, slrList):
    ras1 = arcpy.Raster(r1)
    ras2 = arcpy.Raster(r2)
Related Question