Quantcast
Channel: Forums - Python
Viewing all articles
Browse latest Browse all 2485

Help with dictionary default value syntax.

$
0
0
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.

Code:

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.)

Code:

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:

Code:

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:
Code:

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

Viewing all articles
Browse latest Browse all 2485

Trending Articles