<Another> Dictionary Question

Gabriel Genellina gagsl-py at yahoo.com.ar
Fri Feb 9 00:36:52 EST 2007


At Friday 9/2/2007 00:50, you wrote:

>Hey Gabriel,

Please keep posting on the list - you'll reach a whole lot of people there...

>Thanks again for the help.......  but im still having some issues.....
>For some reason the  "domsrch.search(key)" is pointing to a memory 
>reference....... so when I do this:
>[...]
>          print domsrch.search(key)
>          if domain == domsrch.search(key):
>             utp.write(key + '\n')
><_sre.SRE_Match object at 0xb7f7b0e0>

This is the standard str()/repr() of an object that doesn't care to 
provide it's own __str__/__repr__ method.

Let's look at the docs. From 
http://docs.python.org/lib/re-objects.html we see that the search() 
method returns a MatchObject. And what's that...? See 
http://docs.python.org/lib/match-objects.html , you might be 
interested in using group(1)
But doing the search that way, means that you must traverse the dict 
many times, once per domain searched. Doesn't look so good. Two approaches:

1) Build a specific re that matches exactly all the domains you want 
(and not others). This way you don't even need to use any method in 
the returned MatchObject: simply, if it's not None, there was a 
match. Something like this:

<code>
domains = ["yahoo.com", "google.com", "gmail.com"]
domainsre = "|".join(["(%s)" % re.escape("@"+domain)])
# that means: put an @ in front of each domain, make it a group, and 
join all groups with |
# (\@yahoo\.com)|(\@google\.com)|(\@gmail\.com)
domsrch = re.compile(domainsre, re.IGNORECASE)

for key in d1:
   if domsrch.search(key):
     print key
</code>

2) Forget about regular expressions; you already know they're email 
addresses, just split on the @ and check the right side against the 
desired domains:

<code>
domains = ["yahoo.com", "google.com", "gmail.com"]
domainsSet = set(domains)

for key in d1:
   name, domain = key.split("@",1)
   if domain.lower() in domainsSet:
     print key
</code>


-- 
Gabriel Genellina
Softlab SRL 


	

	
		
__________________________________________________ 
Preguntá. Respondé. Descubrí. 
Todo lo que querías saber, y lo que ni imaginabas, 
está en Yahoo! Respuestas (Beta). 
¡Probalo ya! 
http://www.yahoo.com.ar/respuestas 




More information about the Python-list mailing list