sequence is a string, and gene is the the word that i gotta find..
im new to python.. and so far i got this:

the list always ends with a -1 at the very end...how do i get rid of that..

def find_genes(gene,sequence):
    list= []
    
    count = sequence.count(gene)
    find = sequence.find(gene)
    list.append(find)
    
    while find > -1:
        find = sequence.find(gene,find+1)
        list.append(find)  
        
    return count,list

Recommended Answers

All 4 Replies

well this is a way to atleast find a word and its position out.

from __future__ import print_function
list= ["gene","fooo","spam","spat","foo","gene","geni","spilat"]
print([(count,gene) for count,gene in enumerate(list)if gene=="gene"], end="")

##output
[(0, 'gene'), (5, 'gene')]

Hope it helps
;)

wow, havent learned what u just wrote yet :P
but thanks anyways.

now i can't figure out what does find+1 means..

find.sequence.find(gene.find+1)

now i can't figure out what does find+1 means..

A clue most programming start indexing/counting at 0.

def find_genes(gene,sequence):
    my_list= []  #Dont use list as a varible name,it`s a python keyword

    count = sequence.count(gene)
    find = sequence.find(gene)
    my_list.append(find)

    while find > -1:
        find = sequence.find(gene,find+1)
        my_list.append(find)
        return count,my_list  #Move to remove -1

text = """\
My name is Fred Flintstone and I am a famous TV
star.I have as much authority as the Pope,I
just don't have as many people who believe it.
Fred
"""
gene = 'Fred'

count,index_word =  find_genes(gene,text)
print '%s occurred %s times,at index %s' % (gene,count,index_word)

thanks so much!! helped alot!
I suppose find+1 means to the tell the program to find the next one?

A clue most programming start indexing/counting at 0.

def find_genes(gene,sequence):
    my_list= []  #Dont use list as a varible name,it`s a python keyword

    count = sequence.count(gene)
    find = sequence.find(gene)
    my_list.append(find)

    while find > -1:
        find = sequence.find(gene,find+1)
        my_list.append(find)
        return count,my_list  #Move to remove -1

text = """\
My name is Fred Flintstone and I am a famous TV
star.I have as much authority as the Pope,I
just don't have as many people who believe it.
Fred
"""
gene = 'Fred'

count,index_word =  find_genes(gene,text)
print '%s occurred %s times,at index %s' % (gene,count,index_word)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.