[Tutor] Finding Match in nexted tuple

Tim Johnson tim at johnsons-web.com
Wed Apr 21 21:32:36 EDT 2004


* Magnus Lycka <magnus at thinkware.se> [040421 17:04]:
> Tim Johnson wrote:
> > Let's say I have a list of zip codes returned from a MySQLdb query
> > zip_code_list = (('84536',), ('85003',), ('85004',), ('85006',),
> >                  ('85007',), ('85008',))
> > # and I want to look for a match for a value like '85004'
> 
> If you know that the format is exactly like that, you can
> simply do:
> 
> if ('85004',) in zip_code_list:
>     print "Found!"
 
  Good tip..

> ..but maybe that's not what you intended? If '85004' might
> just be in an arbitrary position in a sequence of tuples with
> more columns than the zip codes, you could for instance do:
> 
> if '85004' in str(zip_code_list):
>     print "Found!"
 
  Eureka! That's what I meant by a 'pythonesque' solution.

  I must ask: what is the overhead in the call to str?
  
  I presume that str(zip_code_list) is being handled by compiled binary 
  ("C") code - so must not be too much, I bet.

  Thank you Magnus.
  tim

> -- 
> Magnus Lycka, Thinkware AB
> Alvans vag 99, SE-907 50 UMEA, SWEDEN
> phone: int+46 70 582 80 65, fax: int+46 70 612 80 65
> http://www.thinkware.se/  mailto:magnus at thinkware.se

-- 
Tim Johnson <tim at johnsons-web.com>
      http://www.alaska-internet-solutions.com



More information about the Tutor mailing list