[Tutor] Quick question

Michael Langford mlangford.cs03 at gtalumni.org
Fri Sep 21 21:12:25 CEST 2007


Use the .get method of the dict to return a nonzero value (say None or -1)
when it can't find an item. That will half your test cases. Example of .get
below:

     --Michael

>>> foo = {}
>>> foo['d']=0
>>> foo['a']=1
>>> if(foo.get('a',1)==0 and foo.get('q',1)==0): print foo
... else: print "adsflkj"
...
adsflkj
>>> foo['q'] = 0
>>> if(foo.get('a',1)==0 and foo.get('q',1)==0): print foo
... else: print "adsflkj"
...
adsflkj
>>> foo['a'] = 0
>>> if(foo.get('a',1)==0 and foo.get('q',1)==0): print foo
... else: print "adsflkj"
...
{'a': 0, 'q': 0, 'd': 0}
>>>




-- 
Michael Langford
Phone: 404-386-0495
Consulting: http://www.TierOneDesign.com/
Entertaining: http://www.ThisIsYourCruiseDirectorSpeaking.com

On 9/21/07, Tino Dai <tinoloc at gmail.com> wrote:
>
> Is there a more pythonic way of doing this:
>
>   if queuePacket.has_key('procSeq') and \
>   queuePacket.has_key('opacSeq') and \
>   queuePacket.has_key('keySeq') and \
>   len(queuePacket['procSeq']) == 0 and \
>   len(queuePacket['opacSeq']) == 0 and \
>  len(queuePacket['keySeq']) == 0:
>
>
> ?
>
> -Thanks,
> Tino
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20070921/5da42e1f/attachment.htm 


More information about the Tutor mailing list