[Tutor] Splitting text

Matthew White mwhite3 at ttsd.k12.or.us
Thu Jun 29 22:06:54 CEST 2006


Hello Appu,

You can use the count() method to find the number of occurances of a
substring within a string:

>>> a = 'TCCCTGCGGCGCATGAGTGACTGGCGTATTTAGCCCGTCACATTTA'
>>> a.count('ATTTA')
2

-mtw

On Thu, Jun 29, 2006 at 12:45:06PM -0700, Apparao Anakapalli (appara_akp at yahoo.com) wrote:
> hello all:
> 
> I have a question and I do not know how I can work it
> out. 
> 
> 
> I have a file of sequences
> >a
> TCCCTGCGGCGCATGAGTGACTGGCGTATTTAGCCCGTCACATTTA'
> >b
> CCTGCGGCGCATGAGTGACTGGCGTATTTAGCCCGTCACAATTTAA'
> ....
> 
> (10 K)
> 
> 
> pattern = 'ATTTA'
> 
> I want to find the pattern in the sequence and count.
> 
> For instance in 'a' there are two 'ATTTA's. 
> 
> How can I do that. 
> 
> One approach tried:
> import re
> 
> pat = 'ATTTA'
> if re.search(pat,a): 
>          print 'yes'
> 
> By counting number of yeses I thought I can answer the
> question. However, the above approach only looks for
> the first instance of pat and says yes and moves to
> next. 
> 
> 
> The other way:
> a.find(pat)
> 
> This also looks for first one and reports the position
> of chracter. 
> 
> 
> Could any one suggest the best way to cound the number
> of patterns. 
> 
> 
> Thank you
> appu         
> 
> 
> __________________________________________________
> Do You Yahoo!?
> Tired of spam?  Yahoo! Mail has the best spam protection around 
> http://mail.yahoo.com 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor


More information about the Tutor mailing list