Home > Net >  Finding the index for multiple characters in a string
Finding the index for multiple characters in a string

Time:09-27

I'm trying to find a way to print the index of each 'A' in this DNA string.

Tried:

dna = 'ATCACGACGTGACGGATCGAGCAGTACG'

for nuc in dna:

    if nuc == 'A':
        print (dna.index(nuc))

Output:

0
0
0
0
0
0
0
0

Which I understand is because the index of the first 'A' is 0. What I'm trying to figure out is if it's possible to use a for loop to print the index of each individual 'A' in the string (so I know where each 'A' is in the string).

CodePudding user response:

One simple way is to use enumerate:

dna = 'ATCACGACGTGACGGATCGAGCAGTACG'

output = [i for i, x in enumerate(dna) if x == 'A']
print(output) # [0, 3, 6, 11, 15, 19, 22, 25]

CodePudding user response:

What is happening is that when your loop encounters an 'A' you call dna.index which will always return the first instance of the character in the sequence when called without the additional start parameter.

A work around would be to keep track of the index your loop is currently at and print that when you encounter the matching character.

For that you can use the range, or enumerate function:

for i in range(len(dna)):
    if dna[i] == "A":
        print(i)

or

for i, nuc in enumerate(dna):
    if nuc == "A":
        print(i)

The second example is essentially the same as j1-lee's answer, without the list comprehension. For the sake of brevity and readability I would always recommend using list comprehension like in j1-lees answer.

  • Related