python - UnboundLocalError: when calling function output -


i want convert list of values 0, 1 , 2 2,2; 1,2 , 1,1. wrote function supposed doing that:

def dec2(code):                           if code == 0:         ret_val = '2','2'     elif code == 1:          ret_val = '1','2'     elif code == 2:          ret_val = '1','1'     else:         ret_val = '0','0'     return ret_val 

it seems working fine long run on ready made lists:

in [87]: a=[1,2,1,2]  in [88]: b=[dec2(x) x in a]  in [89]: b out[89]: [('1', '2'), ('1', '1'), ('1', '2'), ('1', '1')] 

however, when try use substitute dictionary values, unboundlocalerror.:

in [82]: gtps out[82]: {11: [1, 2, 1, 2], 22: [2, 2, 1, 1], 33: [1, 9, 0, 2]} [86]: k,v in gtps.iteritems(): ....:     tmp=[dec2(x) x in v] ....:     all[k]=tmp ....: ....: --------------------------------------------------------------------------- unboundlocalerror                         traceback (most recent call last)  unboundlocalerror: local variable 'ret_val' referenced before assignment 

any ideas?

the code posted cannot under circumstances raise unboundlocalerror, have mixed in ipython session there.

i'd use instead:

_code_map = {0: ('2', '2'), 1: ('1','2'), 2: ('1','1')} def dec2(code):     return _code_map.get(code, ('0', '0')) 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -