AnsweredAssumed Answered

Help with dictionary default value syntax.

Question asked by armein01 on Nov 19, 2013
Latest reply on Nov 22, 2013 by Wayne_Whitley
I have tried all types of syntax,  tried to incorporate an if , which throws an error something about lists not being hashable.  (or something),  but Im inputting a dictionary.

I set up the dictionary.

rtOFDict = dict([((ofR.OBJECTID, ofF.name),ofR.getValue(ofF.name)) for ofF in arcpy.ListFields("BufferOFTble") for ofR in arcpy.SearchCursor("BufferOFTble")])


This gives me:

>>> print rtOFDict
{(1, u'VALUE_3'): 165321.0, (1, u'OBJECTID'): 1, (1, u'VALUE_1'): 386856.0, (1, u'OBJECTID_1'): 1, (1, u'VALUE_2'): 1770579.0}


When there is a route without any occurence of VALUE *  I get a Key Error.  So that is why I want to set a Default.  Knowing my data, I only need a default on Value_0 .  (This syntax is for the if statement that wont work.)

rtFrstd = rtOFDict[(1,'VALUE_2')] rtOpn = rtOFDict[(1,'VALUE_1')] rtNHbt = rtOFDict.get('VALUE_0'[,default]) rtOFDict['Value_0'] if 'Value_0' in rtOFDict, else default frstPrcnt = rtFrstd / (rtFrstd + rtOpn + rtNHbt)


I also tried:

rtNHbt = rtOFDict.get('VALUE_0', 1)


Which always asigns 1 or default to the variable.

I see some syntax in an online reference:

dict.get(key[, default]) ??? value

which leads me to belive that:
rtNHbt = rtOFDict.get('VALUE_0'[, 1] 


should work, but I get:
Invalid Syntax and it points me to the comma  VALUE_0 [,

If you can help me understand this I would be very appreciative!
Alicia

Outcomes