Dictionary issue

1809
23
02-09-2022 10:13 AM
CCWeedcontrol
Occasional Contributor III

I have the following script and the issues I am having is that the PermitNum can have multiple Par (par field) numbers but my current script will only populate the Parel field with just one Par attribute, it doesn't transfer each parcel that corresponds with each permit number. How can I adjust my current code to transfer each parcel number?

fc_dest = "D:/Temp/Cases"

sourceFC = "D:/Temp/Casestableview"
  
#Use Dictionary to update fc fileds.
sourceFieldsList = ['PermitNum','Par','AppS','AppT', 'SubT','Desc', 'MainApp', 'PA', 'Status', 'AppStatus', 'DecDate',]#,'ProjectInfo', 'Contractor', 'Subdivision', 'TwnRngSec', 'LivingSpace', 'TotalSqFt', 'Value_', 'Valuation', 'Status']  
  
# Use list comprehension to build a dictionary from a da SearchCursor  
valueDict = {r[0]:(r[1:]) for r in arcpy.da.SearchCursor(sourceFC, sourceFieldsList)}  

  
updateFieldsList = ['PermitNum','Par','AppS','AppT', 'SubT','Descr', 'MainApp', 'PA', 'Status', 'AppStatus', 'DecDate']  
  
with arcpy.da.UpdateCursor(fc_dest, updateFieldsList) as updateRows:  
    for updateRow in updateRows:  
        # store the Join value of the row being updated in a keyValue variable  
        keyValue = updateRow[0]  
        # verify that the keyValue is in the Dictionary  
        if keyValue in valueDict:  
            # transfer the values stored under the keyValue from the dictionary to the updated fields.  
            for n in range (1,len(sourceFieldsList)):  
                updateRow[n] = valueDict[keyValue][n-1]  
            updateRows.updateRow(updateRow) 
  
del valueDict

 

Book4 - Excel.png

 

0 Kudos
23 Replies
DonMorrison1
Occasional Contributor III

You need to figure out how to uniquely identify each record and make that your dict key. From you example it looks like a combination of  'PermitNum' and 'Apps' is unique.   In that case I would make my dict look like this:

valueDict = {(r[0],r[2]): r for r in arcpy.da.SearchCursor(sourceFC, sourceFieldsList)}  

 

 

 

0 Kudos
CCWeedcontrol
Occasional Contributor III

I thought that's what line 9 was doing?

My unique identifers are PermtNum and Par fields, the Apps field is just a date field. Unless I am not understanding the dictionary correctly...?

valueDict = {r[0]:(r[1:]) for r in arcpy.da.SearchCursor(sourceFC, sourceFieldsList)}

0 Kudos
DonMorrison1
Occasional Contributor III

I think your dict is being built with "PermitNum" as the key - and since that field is not unique the dictionary entry is being overwritten. 

Just curious - in your "I need the following" graphic, how do you know which values to put in the 'Par' column? Since 'PermitNum' is not unique you must be using some other piece of information (I assumed it was the date field but it appears that is wrong).

0 Kudos
CCWeedcontrol
Occasional Contributor III

PermitNum should be the unique identifier but as you can see PermitNum has duplicates because I don't have no other Unique Identifier.

0 Kudos
by Anonymous User
Not applicable

Based on your description your structure looks something like: {  PermtNum : (Par, 'AppS','AppT', 'SubT','Descr', 'MainApp', 'PA', 'Status', 'AppStatus'), ...} ?

If it is only adding one, it is because it is stopping at the first matched key and using the first set of values.  You need to have a loop iterating over the value sets of that matched key.

I find it helpful to print out the dictionary and paste some of it as a code comment so you can refer to it visually while you are coding.  Give it a try-it also helps those troubleshooting with visualizing the data since we can't debug.

I'd also suggest using just row in your cursor so your not using the same name as the updateRow method.

updateRows.updateRow(updateRow) 

 

0 Kudos
CCWeedcontrol
Occasional Contributor III

ok, based on what you are describing I think that is my problem. I guess I am not sure how to iterating loop over the vale sets that match. Do you have an example?

0 Kudos
by Anonymous User
Not applicable

I think that @DonMorrison1 is right in that it is overwriting the key's values with data instead of appending it to a list for the key. Printing out the dictionary will give us a better idea of what to suggest for a fix. Starting with that and then seeing how the dictionary is structured would help determine the code needed for updating. If you find it is overwriting the values, you'll have to test for a keys existence and either set or append for each row's data in that SearchCursor to get that key: [item, item, item] structure.

0 Kudos
CCWeedcontrol
Occasional Contributor III

Below is what prints out.

 

valueDict = {r[0]:(r[1:]) for r in arcpy.da.SearchCursor(sourceFC, sourceFieldsList)} 

{'2021-0006': ('2957001100', datetime.datetime(2021, 6, 11, 0, 0), None, None, '', None, 'address', 'Active', 'In Progress', None), '2021-0032': ('2957001100', datetime.datetime(2021, 6, 11, 0, 0), None, 'Preliminary Plat', '', None, 'address', 'Active', 'In Progress', None,),


valueDict = {(r[0],r[1]): r for r in arcpy.da.SearchCursor(sourceFC, sourceFieldsList)}

{('2021-0006', '2957001100'): ('2021-0006', '2957001100', datetime.datetime(2021, 6, 11, 0, 0), None, None, '', None, 'address', 'Active', 'In Progress', None), ('2021-0032', '2957001100'): ('2021-0032', '2957001100', datetime.datetime(2021, 6, 11, 0, 0), None, 'Preliminary Plat', '', None, 'address', 'Active', 'In Progress', None),
0 Kudos
DonMorrison1
Occasional Contributor III

The dict is exactly what I would expect. But, I still don't understand your objective - and will ask this again:

Just curious - in your "I need the following" graphic, how do you know which values to put in the 'Par' column? 

0 Kudos