It is still only kicking out the first year (mean1945.tif). The loop stopped at 1946.
My raster list is short at the moment, I used only a few months from the years 1945-1949 in my trial folder.
import arcpy
import os
ws_in = r'C:\Users\dzelmanfahm\Desktop\DEM_Trials'
ws_out = r'C:\Users\dzelmanfahm\Desktop\OUTPUT_DEM_TRIALS'
arcpy.env.workspace = ws_in
ras_names = arcpy.ListRasters()
print(ras_names)
arcpy.CheckOutExtension("Spatial")
for year in range(1945, 1950):
print("\nyear:{}".format(year))
lst_ras_year = [ras_name for ras_name in ras_names if str(year) in ras_name]
print("raster for this year:{}".format(lst_ras_year))
out_name = "mean{}.TIF".format(year)
out_name_path = os.path.join(ws_out, out_name)
print("out_name_path:{}".format(out_name_path))
mean_ras = arcpy.sa.CellStatistics(lst_ras_year, statistics_type = "MEAN")
mean_ras.save(out_name_path)
arcpy.CheckInExtension("Spatial")
['pptApr1947.tif', 'pptApril1946.tif', 'pptDec1946.tif', 'pptDec1948.tif', 'pptFeb1945.tif', 'pptJune1946.tif', 'pptJune1947.tif', 'pptNov1945.tif', 'pptNov1948.tif', 'pptNov1949.tif', 'pptOct1945.tif', 'pptOct1949.tif']
year:1945
raster for this year:['pptFeb1945.tif', 'pptNov1945.tif', 'pptOct1945.tif']
out_name_path:C:\Users\dzelmanfahm\Desktop\OUTPUT_DEM_TRIALS\mean1945.TIF
year:1946
raster for this year:['pptApril1946.tif', 'pptDec1946.tif', 'pptJune1946.tif']
out_name_path:C:\Users\dzelmanfahm\Desktop\OUTPUT_DEM_TRIALS\mean1946.TIF
Thank you again!