Reputation: 59
I am trying to write the code to output the index of each location that a word occurs in a string that has no spaces.
Specifically the gene: 'act' within the sequence: 'avdgagtactsdfactactfgactlkact'.
The output should be:
Locations of gene: [7, 13, 16, 21, 26]'
Please help
Upvotes: 0
Views: 49
Reputation: 21749
I would do like this:
j = 'avdgagtactsdfactactfgactlkact'
[i for i, x in enumerate(j) if j[i:i+3] == 'act']
[7, 13, 16, 21, 26]
Upvotes: 1
Reputation: 7430
An answer with regular expressions:
import re
gene = 'act'
sequence = 'avdgagtactsdfactactfgactlkact'
[m.start() for m in re.finditer(gene, sequence)]
Upvotes: 0
Reputation: 474161
Regular expression match object has the handy .start()
method:
In [1]: import re
In [2]: s = "avdgagtactsdfactactfgactlkact"
In [3]: [m.start() for m in re.finditer("act", s)]
Out[3]: [7, 13, 16, 21, 26]
Upvotes: 3
Reputation: 61930
One alternative could be to use a list comprehension:
sequence = 'avdgagtactsdfactactfgactlkact'
gene = 'act'
result = [i for i in range(len(sequence) - len(gene) + 1) if sequence[i:].startswith(gene)]
print(result)
Output
[7, 13, 16, 21, 26]
Upvotes: 1