Question regarding lists and regex
Paul McGuire
ptmcg at austin.rr._bogus_.com
Thu Nov 9 02:14:34 EST 2006
"Prabhu Gurumurthy" <pgurumur at gmail.com> wrote in message
news:mailman.1959.1163053812.11739.python-list at python.org...
> Here is a simple program, which queries /var/log/daemon on my OpenBSD box
> and gets the list of valid ntp peers.
>
> Questions:
> what is the easiest way for me to create lists on the fly, by that I mean
> like perl
>
> push my @foo, something_from_say_stderr. The reason is as you can ip =
> [""] statement before the for loop, I want to avoid that and use list
> within the second ip loop, where I extract the ip address. Am I confusing?
>
Typically, one initializes a list to be empty, that is [], not [""]. Python
will not read your mind at append time and think "oh! we're appending to a
list and we forgot to create one in the first place, let's make one now." I
guess Perl allows this, but the clarity of including the initialization
statement overrules the convenience of leaving it out.
> regex: I presume this is rather a dumb question, anyways here it comes! as
> you can see from my program, pattIp = r\d{1,3}\.... etc, is there any
> other easy way to group the reptitions, instead of typing the same regex 4
> times.
>
Here's one way, tested at the Python command line:
>>> print r'\.'.join( [r'\d{1,3}']*4 )
\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}
This avoids the pattern duplication, but I think using join is much less
easily recognized as a pattern for an IP address.
> TIA
> Prabhu
>
Some other comments/free advice:
1. I was curious about this line:
pid = int(re.sub(r'\[|\]', "", pidMatch.group()))
You already know pidMatch.group() is going to start with a '[', followed by
an integer string, and end with a ']', otherwise it wouldn't have matched
pidPatt. Instead of whacking this with another re-type call, how about just
some simple string slicing:
pid = pidMatch.group()[1:-1]
2. No real need to keep count of the found ip's, just use len(ip) to tell
you how many entries there are in the list (especially once you convert to
intializing with an empty list).
3. Similarly, you'll be able to remove the 'if len(x)' test when printing
out the contents of the ip list if you init with [] instead of [""]. Also,
the Python idiom for testing if x is the empty string is usually just 'if
x', not 'if len(x)'.
-- Paul
More information about the Python-list
mailing list