2

I'm trying to work out a function to find specific patterns in a list. For example if we take the list

x = [1,1,2,3,54,3,1]

I want to then check if the pattern y shows up in the list x:

y = [1,1,n,n,n,n,1]

where n represents any number. So in my example it would return True.

I've looked into the any() function and I haven't been able to work much out.

1
  • 3
    The difference in the first three answers you've gotten speaks to the vagueness of what you're asking. I tried to cover all the bases. Commented Mar 28, 2013 at 4:23

6 Answers 6

3
>>> from operator import itemgetter
>>> x = [1, 1, 2, 3, 54, 3, 1]
>>> itemgetter(0,1,6)(x) == (1, 1, 1)
True

How is y really defined. Obviously you can't have n in there as a placeholder? Could your use None perhaps?

Sign up to request clarification or add additional context in comments.

2 Comments

@ealfonso The positions can be changed, the OP didn't state how they are given though.
The positions can be changed, but they will still be fixed, and they can not match a sublist of the original.
1

I think you are confused about what any means. It is used to check a sequence of values, and see if any of them is "true". That's not related to finding out if a value is "any number" or "any of these possibilities".

If you have a fixed, finite set of possibilities that you want to consider, then what you really want to know is whether your candidate value is in that set:

x in {1, 2, 3, 4, "hi mom"} # returns whether x is any of those values

But "any number" is not a finite set. First off, you need to define what you mean by number; and then you need to perform the appropriate test. It sounds like what you are trying to do is check whether the value is an integer. In other words, you are concerned with the type of the values in the list.

If you already know they're all integers, then there's nothing to test; if you don't care what the value is, then just don't consider it when you make your checks. But if you need to be sure it's an integer, then the way to do that is

isinstance(x, int) # returns whether x is an `int`

But maybe you have confused me, by giving an example "to-search list" that happens to be the same length as your "pattern", when you actually want to look for the pattern at any point in a longer list.

In that case, you can make a function that does an exact match of the pattern against a list of the same length; and then use any to check whether any pattern-lengthed sublist matches. any is designed to be used with generator expressions, and it looks like this:

def match(a_sublist, the_pattern):
    # put your logic here

def search(the_full_list, the_pattern):
    pattern_length, full_length = len(the_pattern), len(the_full_list)
    return any(
        match(the_full_list[i:i+pattern_length], the_pattern)
        for i in range(full_length - pattern_length)
    )

There are more efficient ways to match, depending on the details of your pattern, that will be inspired by string search algorithms and regular expression engines. But that is getting into much more difficult material - the above should get you started.

1 Comment

That's... incredibly brittle.
1
from itertools import izip, islice
x = [2,1,3,1,1,2,3,54,3,1,5,6,7,1,1,0,0,0,0,1]
y = [1,1,None,None,None,None,1]

print [i for i in xrange(len(x)-len(y)+1) 
         if all(b is None or a==b for a,b in izip(islice(x, i, i+len(y)), y))]

Or more code for easy to understand:

def nwise(x, n):
    for i in xrange(len(x)-n+1):
        yield i, islice(x, i, i+n)

def match(x, y):
    return all(b is None or a==b for a,b in izip(x, y))

print [i for i, xs in nwise(x, len(y)) if match(xs, y)]

Comments

0

i think you want to search for a list whose pattern got matched.. .

x = [[1,1,2,3,54,3,1],[1,2,3,4,5,6,7],[2,4,6,8,10,12,14]]
y = [1,1,None,None,None,None,1] ## or [1,1,'n','n','n','n',1]

for l in x:
    if all(map(lambda x:x[0]==x[1],[x for x in zip(l,y) if x[1] and x[1]!='n'])):
        print l

output:

[1,1,2,3,54,3,1]

Comments

0

This type of problem is well suited to Numpy masked arrays:

import numpy.ma as ma

x = ma.array([1,1,2,3,54,3,1])
y = ma.array([1,1,1,1,1,1,1], mask=[0,0,1,1,1,1,0])

print x==y           # [True True -- -- -- -- True]
print ma.all(x==y)   # True

Of course, the use here may not merit installing and importing numpy, but it has advantages in some situations.

Comments

0
x = [1,1,2,3,54,3,1]
y = [1,1,0,0,0,0,1]
any([i[0]==i[1] for i in zip(x,y)])

1 Comment

Well, I didn't see anything about sublists in the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.