Hi,
So I have come across a challenging situation where I have multiple attribute values, and I want to create nested dictionaries based on certain attributes. So for instance:
{Pipe Diameter: {Pipe Material A: Total length for diameter and material, Pipe Material B: Total length for diameter and material}
import arcpy
import math
import time
#import pandas as pd
import sys
import os
in_fcs = feature class name
workspace = feature class
fcs = []
walk = arcpy.da.Walk(workspace, datatype="FeatureClass")
for dirpath, dirnames, filenames in walk:
for filename in filenames:
#Get feature class name
fcsname = os.path.basename(filename)
name = os.path.splitext(fcsname)
y = name[1].lstrip('.')
if y in in_fcs:
#print y
fcs.append(os.path.join(dirpath, filename))
for fc in fcs:
geometryType = arcpy.Describe(fc).shapeType
#print (geometryType)
fc_fields = []
important_fields = ['OBJECTID', Field A, Field B, FIeld C, Field D]
important_FAvalues = [List of important attribute values in field A]
important_FBvalues = [List of important attribute values in field B]
fcsname = os.path.basename(fc)
name = os.path.splitext(fcsname)
y = name[1].lstrip('.')
print ('\n',y)
i = 0
t = 0
#v = {}
d = {}
allfields = arcpy.ListFields(fc)
for field in allfields:
if field.name in important_fields:
#print (field.name)
fc_fields.append(field.name)
fcFields_len = len(fc_fields)
fc_fields_wLength = fc_fields + ['SHAPE@LENGTH']
ImpA = fc_fields_wLength.index(important_fields[1])
ImpB = fc_fields_wLength.index(important_fields[2]) or fc_fields_wLength.index(important_fields[3])
ImpC = fc_fields_wLength.index(important_fields[4])
ImpD = fc_fields_wLength.index(important_fields[5])
with arcpy.da.SearchCursor(fc, fc_fields_wLength) as scur:
for s in scur:
if s[ImpA] in important_FAvalues and s[ImpB] in important_FBvalues:
i = i + 1
t = t + s[-1]
if s[ImpC] is None:
#d[s[ImpC]] = {s[ImpD]:s[-1]}
pass
elif s[ImpC] is not None:
if int(s[ImpC]) >= 8 and int(s[ImpC]) <= 42:
#d = {}
d[s[ImpC]] = {}
for a, b in d.items():
if a == s[ImpC]:
for e, f in b.items():
if e == s[ImpD]:
f = f + s[-1]
d[s[ImpC]][s[ImpD]] = f
else:
d[s[ImpC]][s[ImpD]] = s[-1]
#print (round(t, 2))
print (d)
So I am unclear on how to accomplish this, but if anyone could help me with this I would greatly appreciate it.