Pattern Matching Over Python Lists
Kirk Strauser
kirk at daycos.com
Tue Jun 17 11:14:45 EDT 2008
At 2008-06-17T05:55:52Z, Chris <chrisspen at gmail.com> writes:
> 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
More information about the Python-list
mailing list