By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,421 Members | 1,244 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,421 IT Pros & Developers. It's quick & easy.

Pattern Matching Over Python Lists

P: n/a
Is anyone aware of any prior work done with searching or matching a
pattern over nested Python lists? I have this problem where I have a
list like:

[1, 2, [1, 2, [1, 7], 9, 9], 10]

and I'd like to search for the pattern [1, 2, ANY] so that is returns:

[1, 2, [1, 2, [6, 7], 9, 9], 10]
[1, 2, [6, 7], 9, 9]
Jun 27 '08 #1
Share this Question
Share on Google+
9 Replies

P: n/a
At 2008-06-17T05:55:52Z, Chris <ch*******@gmail.comwrites:
Is anyone aware of any prior work done with searching or matching a
pattern over nested Python lists? I have this problem where I have a
list like:

[1, 2, [1, 2, [1, 7], 9, 9], 10]

and I'd like to search for the pattern [1, 2, ANY] so that is returns:

[1, 2, [1, 2, [6, 7], 9, 9], 10]
[1, 2, [6, 7], 9, 9]
Hint: recursion. Your general algorithm will be something like:

def compare(list, function):
if function(list):
print list
for item in list:
if item is a list:
compare(item, function)

def check(list):
if list starts with [1, 2] and length of the list 2:
return True
else:
return False
--
Kirk Strauser
The Day Companies
Jun 27 '08 #2

P: n/a
Kirk Strauser:
Hint: recursion. Your general algorithm will be something like:
Another solution is to use a better (different) language, that has
built-in pattern matching, or allows to create one.

Bye,
bearophile
Jun 27 '08 #3

P: n/a
Thanks for your help. Those weren't quite what I was looking for, but
I ended up figuring it out on my own. Turns out you can actually
search nested Python lists using simple regular expressions.
Jun 27 '08 #4

P: n/a
On Jun 17, 1:09 pm, bearophileH...@lycos.com wrote:
Kirk Strauser:
Hint: recursion. Your general algorithm will be something like:

Another solution is to use a better (different) language, that has
built-in pattern matching, or allows to create one.

Bye,
bearophile
Btw, Python's stdlib includes a regular expression library. I'm not
sure if you're trolling or simply unaware of it, but I've found it
quite adequate for most tasks.
Jun 27 '08 #5

P: n/a
On Jun 20, 10:45 am, Chris <chriss...@gmail.comwrote:
On Jun 17, 1:09 pm, bearophileH...@lycos.com wrote:
Kirk Strauser:
Hint: recursion. Your general algorithm will be something like:
Another solution is to use a better (different) language, that has
built-in pattern matching, or allows to create one.
Bye,
bearophile

Btw, Python's stdlib includes a regular expression library. I'm not
sure if you're trolling or simply unaware of it, but I've found it
quite adequate for most tasks.
Kindly consider a third possibility: bearophile is an experienced
Python user, has not to my knowledge exhibited any troll-like
behaviour in the past, and given that you seem to be happy using the
re module not on strings but on lists of integers, may have been
wondering whether *you* were trolling or just plain confused but just
too polite to wonder out loud :-)
Jun 27 '08 #6

P: n/a
On Jun 20, 1:44*am, Chris <chriss...@gmail.comwrote:
Thanks for your help. Those weren't quite what I was looking for, but
I ended up figuring it out on my own. Turns out you can actually
search nested Python lists using simple regular expressions.
Strange?
How do you match nested '[' ... ']' brackets?

- Paddy.
Jun 27 '08 #7

P: n/a
On Jun 20, 1:45*am, Chris <chriss...@gmail.comwrote:
On Jun 17, 1:09 pm, bearophileH...@lycos.com wrote:
Kirk Strauser:
Hint: recursion. *Your general algorithm will be something like:
Another solution is to use a better (different) language, that has
built-in pattern matching, or allows to create one.
Bye,
bearophile

Btw, Python's stdlib includes a regular expression library. I'm not
sure if you're trolling or simply unaware of it, but I've found it
quite adequate for most tasks.
bearophile was talking about matching lists and tuples, not matching
strings.

Python's regular expression module works with characters in strings,
but the same approach can be applied to items in lists and tuples.
Jun 27 '08 #8

P: n/a
On Jun 19, 9:03 pm, John Machin <sjmac...@lexicon.netwrote:
On Jun 20, 10:45 am, Chris <chriss...@gmail.comwrote:
On Jun 17, 1:09 pm, bearophileH...@lycos.com wrote:
Kirk Strauser:
Hint: recursion. Your general algorithm will be something like:
Another solution is to use a better (different) language, that has
built-in pattern matching, or allows to create one.
Bye,
bearophile
Btw, Python's stdlib includes a regular expression library. I'm not
sure if you're trolling or simply unaware of it, but I've found it
quite adequate for most tasks.

Kindly consider a third possibility: bearophile is an experienced
Python user, has not to my knowledge exhibited any troll-like
behaviour in the past, and given that you seem to be happy using the
re module not on strings but on lists of integers, may have been
wondering whether *you* were trolling or just plain confused but just
too polite to wonder out loud :-)
Fair enough. To help you understand the method I used, I'll give you
this hint. It's true that regex on works on strings. However, is there
any way to convert arbitrarily complex data structures to string
representations? You don't need to be an experienced Python user to
answer to this ;)
Jun 27 '08 #9

P: n/a
Fair enough. To help you understand the method I used, I'll give you
this hint. It's true that regex on works on strings. However, is there
any way to convert arbitrarily complex data structures to string
representations? You don't need to be an experienced Python user to
answer to this ;)
As Paddy noted before, your solution has a problem, Regexes can't
match nested parenthesis, so I think your method will have a problem
with nested lists, unless your actual inputs are much simpler than the
general case.

Eli
Jun 27 '08 #10

This discussion thread is closed

Replies have been disabled for this discussion.