473,320 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 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 4300
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...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.