rob g
rob g

Reputation: 59

how to find the index of a reoccurring word in a string without spaces

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

Answers (4)

YOLO
YOLO

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

Franco Piccolo
Franco Piccolo

Reputation: 7430

An answer with regular expressions:

import re

gene = 'act'
sequence = 'avdgagtactsdfactactfgactlkact'

[m.start() for m in re.finditer(gene, sequence)]

Upvotes: 0

alecxe
alecxe

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

Dani Mesejo
Dani Mesejo

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

Related Questions