1

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:

1 Answer 1

3

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)
2
  • Like this, I really should make better use of those itertools in python!
    – Hornbydd
    Oct 26, 2022 at 13:32
  • I had not imported it, that was my problem. It works beautifully now! That is a great tool. Thank you. Oct 27, 2022 at 23:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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