Using dictionary to hold regex patterns?

Benjamin Kaplan benjamin.kaplan at case.edu
Sun Nov 23 15:27:26 EST 2008


On Sun, Nov 23, 2008 at 2:55 PM, Gilles Ganault <nospam at nospam.com> wrote:

> On Sun, 23 Nov 2008 17:55:48 +0000, Arnaud Delobelle
> <arnodel at googlemail.com> wrote:
> >But there is no reason why you should use a dictionary; just use a list
> >of key-value pairs:
> >
> >patterns = [
> >    ("pattern1", re.compile(">.+?</td>.+?>(.+?)</td>"),
>
> Thanks for the tip, but... I thought that lists could only use integer
> indexes, while text indexes had to use dictionaries. In which case do
> we need dictionaries, then?
> --

Lists do use integer indexes. Since you never use the dict[key] syntax, you
don't need key value pairs like that. Instead, the example uses two-item
tuples.


>>> patterns = [("pattern1", re.compile(">.+?</td>.+?>(.+?)</td>")),
("pattern2", re.compile("something else"))]
>>> patterns[0]
('pattern1', <_sre.SRE_Pattern object at 0x3c7a0>)
>>> for pattern, regex in patterns :
...    print pattern + ":" + str(regex)
...
pattern1:<_sre.SRE_Pattern object at 0x3c7a0>
pattern2:<_sre.SRE_Pattern object at 0x35860>



>
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20081123/8f4f9201/attachment-0001.html>


More information about the Python-list mailing list