Using dictionary to hold regex patterns?

John Machin sjmachin at lexicon.net
Sun Nov 23 15:39:04 EST 2008


On Nov 24, 6:55 am, Gilles Ganault <nos... at nospam.com> wrote:
> On Sun, 23 Nov 2008 17:55:48 +0000, Arnaud Delobelle
>
> <arno... 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?

You don't have a requirement for indexing -- neither a text index nor
an integer index. Your requirement is met by a sequence of (name,
regex) pairs. Yes, a list is a sequence, and a list has integer
indexes, but this is irrelevant.

General tip: Don't us a data structure that is more complicated than
what you need.



More information about the Python-list mailing list