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)
-
Where are the k v values in your for loop ?gene– gene2016年08月29日 20:17:25 +00:00Commented Aug 29, 2016 at 20:17
-
As I understand it the k and v values refer to my Key and Value elements in my dictionary : "AllocClmNum","CorrAllocCreVal" in the first set of code.Frederic– Frederic2016年08月29日 20:29:49 +00:00Commented Aug 29, 2016 at 20:29
-
If you get the 'too many values to unpack' error, you need to enclose your k,v variables in brackets so they are treated as a tuple: for (k, v) in ...jon_two– jon_two2016年08月30日 15:43:31 +00:00Commented Aug 30, 2016 at 15:43
-
Also, 'dict' is a reserved word, so is not a good variable name. Try calling it myDict or something else.jon_two– jon_two2016年08月30日 15:45:32 +00:00Commented Aug 30, 2016 at 15:45
-
@jon_two. Thanks for your suggestions. I'll change my 'dict' variable and keep your solution for the 'too many values to unpack' in mind as I progress. Excellent points.Frederic– Frederic2016年08月30日 16:28:11 +00:00Commented Aug 30, 2016 at 16:28
1 Answer 1
a few things:
- when you store your multiple values for each key make sure it a tuple and not a list
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...
-
Thank you, that seems like the solution I am looking for. But I'm not clear on how I change my multiple values for each keys into a tuple rather than a list. Something like : tupleValues = (row[0],row[1])?Frederic– Frederic2016年08月29日 20:09:54 +00:00Commented Aug 29, 2016 at 20:09
-
You just want the values stored in the tuple - not the keyziggy– ziggy2016年08月29日 21:18:17 +00:00Commented Aug 29, 2016 at 21:18
-
dict[Recipient] = (Allocation)ziggy– ziggy2016年08月29日 21:18:30 +00:00Commented Aug 29, 2016 at 21:18
-
what does your dictionary look like when you print it out? maybe i am not understanding it correctlyziggy– ziggy2016年08月29日 21:20:21 +00:00Commented Aug 29, 2016 at 21:20
-
Thanks again. Using your code suggestion above I get the following printed dictionary result :
'AllocClmNum IN [u'1014414', u'1014395', u'1014381']
which is exactly what I want it to return : the values that are associated to the key value. Can these values now be accessed by index? I need to use these values to select rows from another table based on each of these values and then run another calculation.Frederic– Frederic2016年08月30日 12:02:48 +00:00Commented Aug 30, 2016 at 12:02
Explore related questions
See similar questions with these tags.