finding sublist

Ron Adam rrr at ronadam.com
Tue Aug 2 12:23:12 EDT 2005


giampiero mu wrote:
> hi everyone

Hi, you appear to be fairly new to Python, so you might want to take a 
look at the tutorial at Python.org

Kents suggestion to use RE is good.


This should be faster than your example by quite a bit if you don't want 
to use RE.

def controlla(test, size=4):
     # Return substring if a second substring is found.
     # Return None if no match found.

     for i in range(len(test)-size):
         match=test[i:i+size]
         left=test[:i]
         right=test[i+size:]
         if match in left or match in right:
             return match

print controlla('qqqabcdrrabcd')

-->  'abcd'


Here's a few notes on your example for future reference:

Multiple breaks don't work.  The first break will jump out of the loop 
before the other breaks are reached.

Any function that ends without a return will return None.  So you don't 
need to return 'no'.

Cheers,
Ron





More information about the Python-list mailing list