# 使用SearchCursor来遍历要素类 with arcpy.da.SearchCursor(feature_class, ["省级", "县级类"]) as cursor: for row in cursor: province = row[0] county_type = row[1] stats[province][county_type] += 1
# 打印统计结果 print("各省级单位县级类统计结果:") # 打印表头 header = "省级"
for type_name in county_types: header += f"\t{type_name}" header += "\t总计" print(header)
# 打印各省数据 for province, types in stats.items(): # 计算总计时排除"不统计"项 total = sum(count for type_name, count in types.items() if type_name != '不统计') line = province for type_name in county_types: line += f"\t{types[type_name]}" line += f"\t{total}" print(line)
# 计算总计 total_stats = collections.defaultdict(int) for types in stats.values(): for county_type, count in types.items(): total_stats[county_type] += count
# 计算总数时排除"不统计"项 total_sum = sum(count for type_name, count in total_stats.items() if type_name != '不统计')
print("\n总计:") for type_name in county_types: print(f"{type_name}: {total_stats[type_name]}") print(f"总数: {total_sum}")