472,363 Members | 2,039 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,363 software developers and data experts.

Pattern Matching Over Python Lists

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
9 4228
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: BalyanM | last post by:
Hi, I am new to python.I am using it on redhat linux 9. I am interested to run python on a sun machine(SunE420R,os=solaris) with 4 cpu's for a pattern discovery/search program on biological...
176
by: Thomas Reichelt | last post by:
Moin, short question: is there any language combining the syntax, flexibility and great programming experience of Python with static typing? Is there a project to add static typing to Python? ...
3
by: Greg Lindstrom | last post by:
Hello- I'm running Python 2.2.3 on Windows XP "Professional" and am reading a file wit 1 very long line of text (the line consists of multiple records with no cr/lf). What I would like to do is...
9
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python # Matching string patterns # # Sometimes you want to know if a string is of # particular pattern. Let's say in your website # you have converted all images...
4
by: Xah Lee | last post by:
20050207 text pattern matching # -*- coding: utf-8 -*- # Python # suppose you want to replace all strings of the form # <img src="some.gif" width="30" height="20"> # to # <img...
5
by: olaufr | last post by:
Hi, I'd need to perform simple pattern matching within a string using a list of possible patterns. For example, I want to know if the substring starting at position n matches any of the string I...
9
by: Jim Lewis | last post by:
Anyone have experience with string pattern matching? I need a fast way to match variables to strings. Example: string - variables ============ abcaaab - xyz abca - xy eeabcac - vxw x...
8
by: Xah Lee | last post by:
the Python regex documentation is available at: http://xahlee.org/perl-python/python_re-write/lib/module-re.html Note that, i've just made the terms of use clear. Also, can anyone answer what...
2
by: Ole Nielsby | last post by:
First, bear with my xpost. This goes to comp.lang.c++ comp.lang.functional with follow-up to comp.lang.c++ - I want to discuss an aspect of using C++ to implement a functional language, and...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
0
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.