On Thu, Sep 24, 2009 at 6:31 PM, Rhodri James <rho...@wildebst.demon.co.uk> wrote: > On Thu, 24 Sep 2009 19:45:37 +0100, Simon Forman <sajmik...@gmail.com> > wrote: > >> FWIW this problem is too simple (IMHO) for regular expressions. >> Simply carve off the first three digits and check against sets of the >> prefixes you're interested in: >> >> >> #any number starting with these prefixes is not long distance >> local_prefixes = set(['832', '877', '888', '713', '866', '011', '001', >> '281', '800']) >> >> long_distance= {} >> for key1, value1 in x.iteritems(): >> if key1 == 'dest': >> if value1[:3] not in local_prefixes: >> long_distance[key1] = value1 > > You need to allow for the potential leading 1, which can be done with > a bit of straightforward string slicing but still puts REs more > sensibly in the running. > > -- > Rhodri James *-* Wildebeest Herder to the Masses > -- > http://mail.python.org/mailman/listinfo/python-list >
Wow, I was looking right at it ('1?') and I missed it. I need more sleep. This is slightly less win: for key1, value1 in x.iteritems(): if key1 == 'dest': n = value1[0] == '1' if value1[n:n + 3] not in local_prefixes: long_distance[key1] = value1 (MRAB's comment about the foolishness of iterating through the dict but testing for key == string_literal still applies, of course.) Depending on what the OP's doing I might recommend just "normalizing" the phone numbers before processing them, (i.e. stripping off the '1's and possible breaking them up into tuples ('nnn', 'nnn', 'nnnn'). ~Simon -- http://mail.python.org/mailman/listinfo/python-list