finding sublist

Kent Johnson kent37 at tds.net
Tue Aug 2 11:00:57 EDT 2005


giampiero mu wrote:
> hi everyone
> 
> my target is implement a function controlla(string - a binary string-)
> that check if there is in a string two equal not overlapping
> subsequences at least of length limitem:

You can do this with a regular expression:

 >>> import re
 >>> r=re.compile(r'(?P<seq>.{4,}).*(?P=seq)')
 >>> r.match('abcaoeaeoudabc')
 >>> r.match('abcdaeaeuabcd')
<_sre.SRE_Match object at 0x008D67E0>
 >>> _.group(1)
'abcd'
 >>> r.match('abcdefgaeaeuabcdefg')
<_sre.SRE_Match object at 0x008D68E0>
 >>> _.group(1)
'abcdefg'

Kent


> 
> my code:
> 
> def controlla(test):
> #       print test
>         limitem=4
> 	lunghezz=len(test)
> 
>         l1=lunghezz-limitem+1
>         l2=lunghezz-limitem+1
>         f=0
> 
>         for ii in range(l1):
>                 for kk in range(l2):
>                         t1=test[ii:ii+limitem]
>                         t2=test[kk:kk+limitem]
>                         if abs(ii-kk)>=limitem :
>                                 if t1==t2 :
>                                         f=1
>                                 if f==1 :
>                                         break
>                                         break
> 					break
>         if f==0 :
>                 return test
>         else:
> 		return 'no'
> 
> 
> the question is: Is there a faster way doing that? I don't know,
> changing string into list or array, using map, or module, using
> different loop, regular expression,funcional programming , list
> comprehensions , sets, different looping techniques, i dont
> know.......(!)
> 



More information about the Python-list mailing list