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] 9 4319
At 2008-06-17T05:55:52Z, Chris <ch*******@gmai l.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
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
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.
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.
On Jun 20, 10:45 am, Chris <chriss...@gmai l.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 :-)
On Jun 20, 1:44*am, Chris <chriss...@gmai l.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.
On Jun 20, 1:45*am, Chris <chriss...@gmai l.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.
On Jun 19, 9:03 pm, John Machin <sjmac...@lexic on.netwrote:
On Jun 20, 10:45 am, Chris <chriss...@gmai l.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 ;)
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
sequence(genomic sequence).I want to write the python code so that it
utilizes all the 4 cpu's.Moreover do i need some other libraries.
Kindly advice.
Thanks
|
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?
Thank you,
--
greetz tom
|
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 scan for the occurrence of a specific
pattern of characters which I expect to repeat many times in the file.
Suppose I want to search for "Start: mm/dd/yy" and capture the mm/dd/yyyy
data for processing each time I find it. This is the type of...
|
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 files from gif
# format to png format. Now you need to change the
# html code to use the .png files. So, essentially
|
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 src="some.png" width="30" height="20">
# in your html files.
| |
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 have a list, as
below:
sentence = "the color is $red"
patterns =
pos = sentence.find($)
|
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 matches abc
|
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 is the precise terms of license of the
official python documentation? The official python.org doc site is not
clear.
Note also, that the regex syntax used by Perl is the same as Python.
|
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 I'd like the attention of fp as well
as C++ gurus if available.
The language I'm implementing - PILS - is dynamically
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |