I have two lists and I would like to find a way to select them based on names.
In list one I have a series of tif files:
list1=['LT50300281984137PAC00_sr_band1.tif',
,'LT50300281984137PAC00_sr_band2.tif'
'LT50300281984137PAC00_sr_band3.tif','LT50300281994260XXX03_sr_band1.tif',
'LT50300281994260XXX03_sr_band2.tif',
'LT50300281994260XXX03_sr_band3.tif']
in list two I have two files:
list2=[LT50300281984137PAC00_mask.tif,LT50300281994260XXX03_mask.tif]
I want to exectue a process (which I won't ask how to do here) for files in list one which start with LT50300281984137PAC00
to the file in list 2 which starts the same way, and the same for the files which start with LT50300281994260XXX03
.
I think it will go something like:
#select the files based on names
for i in list1:
if i[0:20] in list2:
#this code executes the process I want ONLY if the naming matches
for b in list1:
arcpy.gp.ExtractByMask_sa(b,list2,outraster)
else:
print('Done')
So basically if the file names in list 1 match those in list 2 I want to execute my process otherwise print done
Edit:
New code being tried is:
import arcpy
arcpy.env.workspace=r'F:\Sheyenne\Atmospherically Corrected Landsat\hank_practice'
list1 = arcpy.ListRasters("*band*")
list2 = arcpy.ListRasters("*_nocloud.tif")
for raster in list1:
rasterName = raster
rasterMask = "" #initialize your variable to be populated in the inner loop
for mask in list2:
if mask.startswith(rasterName):
rasterMask = mask
break #break out of the inner loop - we have a match
if rasterMask != "": #just to make sure we have something
#generic naming
outraster = rasterName.replace(".tif", "_masked.tif")
arcpy.gp.ExtractByMask_sa(rasterName,rasterMask,outraster)
startwith()
in a loop? Like if blah.startswith(anotherstring) add to a sub list to be processed. Info: tutorialspoint.com/python/string_startswith.htm