[GIS] Using arcpy dictionaries to select attributes from a different layer

arcpydictionaryselect-by-attribute

I'd like to incorporate the use of dictionaries to access data between tables. So far I have been able to create the dictionaries that I need from my feature class fields. I have one dictionary that lists multiple Property values for one key [(u'1010305', [u'1014414', u'1014395', u'1014381']), (u'1010306', [u'1014396', u'1014415', u'1014432', u'1014414', u'1014395', u'1014382', u'1014381']).... I would like to use the values associated to each key to select rows ("select by attribute") in another table. While I can call the values by key or item in the command window I can't seem to incorporate them into a SelectLayerByAttribute function. This is the code so far :

# Purpose: Create a dictionary of values that combines Recipient Claims to Allocating Claims from the BufferRecipientClaims feature class.
# Usage: No arguments needed.
# Source : Python for ArcGIS, Laura Tateosian, 2015, Springer, Chapter 18 p.349.

import arcpy, numpy

arcpy.env.workspace = "CURRENT"
fc = "RecipientBufferClaims"

# Populate the dictionary,
# accumulate a list of Recipient Claims (Value) for each Allocation Claim (Key) type.
dict = {}
SearchCurs = arcpy.da.SearchCursor("RecipientBufferClaims",["RecClmNum","AllocClmNum"])
for row in SearchCurs:
    Recipient = row[0]
    Allocation = row[1]
    if dict.has_key(Recipient):
        dict[Recipient].append(Allocation)
    else:
        dict[Recipient] = [Allocation]
del SearchCurs

All of which works. I'd like to use something like the following to select rows in a new table using the returned values for the key "1010305" but I'm getting an error message

for k, v in dict.values["1010305"]:
    AllocationVariable = AllocationVariable(v)
    print "the allocation claim numbers are  {0} ".format(AllocationVariable)
    arcpy.selectLayerByAttribute(fc,""" "AllocClmNum" = AllocationVariable """)

Runtime error 
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ValueError: too many values to unpack

Edit: I did manage to get this to update another table, unfortunately it did so without filtering the values associated to the key 1010305, but I'm getting closer (code below).

with arcpy.da.UpdateCursor("ActualTbleCopy2SortedAllocationClaims",["AllocClmNum","CorrAllocCreVal"]) as UpdateCurs:
    for row in UpdateCurs:
        for k, v in dict.items():
            RecipientNumber = str(k)
            if RecipientNumber == ("1010305"):
                row[1] = 9999
                UpdateCurs.updateRow(row)

Best Answer

a few things:

  1. when you store your multiple values for each key make sure it a tuple and not a list
  2. After you correctly stored your key value(tuple) pairs in your dictionary. Try calling the dictionary like this

    for k,v in dict.items():
        if k = "1010305":
            qry = "AllocClmNum IN {}".format(v)
            arcpy.selectLayerByAttribute(fc, qry)
    

EDIT: saving you v values to a variable

for k,v in dict.items():
        if k = "1010305":
            values = tuple(v)
            qry = "AllocClmNum IN {}".format(values)
            arcpy.selectLayerByAttribute(fc, qry)

to index you would just do something like this:

ex: print values[1]
    print values[3] etc...
Related Question