On Fri, Oct 23, 2009 at 11:05 AM, John wrote:
> mydict = {'int': 'I', 'char':'C', 'bool':'B'}
> for fldType in fieldList:
> try:
> fld = mydict[fldType]
> except:
> fld = '?'
Use
fld = mydict.get(fldType, '?')
> I also considered some sort of lambda function as
>
>
myDict.get(fldType, None)
this will output the value from myDict if the key exists, if not it will
return None
Thanks this looks perfect - did not know dict.get()
You may also want to consider {}.setdefault -- that would collect the
fldType entries not defined.
>>> a = {'a':1,'b':2}
>>
On Friday 23 October 2009 08:05:29 am John wrote:
> I'm using python 2.5
>
> I have a long list of if, elif, else. I always thought it was very NOT
> pythonic. It's easy to read but not pretty.
>
> for fldType in fieldList:
> if "int" in fldType:
> fld = "I"
> elif "char" in fldType :
>
On Fri, Oct 23, 2009 at 9:05 AM, John wrote:
> I'm using python 2.5
>
> I have a long list of if, elif, else. I always thought it was very NOT
> pythonic. It's easy to read but not pretty.
>
> for fldType in fieldList:
> if "int" in fldType:
> fld = "I"
> elif "char" in fldType :
>
I'm using python 2.5
I have a long list of if, elif, else. I always thought it was very NOT
pythonic. It's easy to read but not pretty.
for fldType in fieldList:
if "int" in fldType:
fld = "I"
elif "char" in fldType :
fld = "C"
elif "bool" in fldType :
fld = "B" .