在 Arcpy 中使用列表命名输出文件

Using a list to name output files in Arcpy

我正在使用 NLCD 数据集,并尝试根据县边界提取更改的像素(按掩码提取)。我已经写了一个小脚本来完成这项工作,但我想知道我是否可以以某种方式使用边界名称作为我的输出名称,因为目前,输出只是 1, 2, 3, ... 正如您将看到的,我正在使用一个计数器。我试过 counter = ctyList 但它不起作用。 这是脚本:

import os
import arcpy
from arcpy import env
from arcpy.sa import *
env.workspace = "C:\Users\sr\Desktop\Sam\AllCounties"
ctyList = arcpy.ListFeatureClasses ("*.shp")
inRaster = "nlcdrecmosaic"
counter = 1
for shp in ctyList:
 out= arcpy.sa.ExtractByMask(inRaster,shp)
 out.save("C:\Users\sr\Desktop\Sam\AllCounties\out\mskd"+ str(counter))
 counter = counter + 1     

非常感谢您的帮助。 山姆

for shp in ctyList:
    county_name = shp.split('.')[0]  # Split the file name at the '.' to get just the name without the extension 
    out= arcpy.sa.ExtractByMask(inRaster,shp)
    out.save("C:\Users\sr\Desktop\Sam\AllCounties\out\mskd"+ county_name