Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
getting index
#1
Hello,
help me get the index of each letters of the string below.


a='GTCCGTTTAGAAGGTTT'
I tried the following but what I get is the index of the latter alphabet has the same index as the one which is visited earlier.
I need index for each one of 'em.


a='GTCCGTTTAGAAGGTTT'
for i in a:
    print a.index(i)
Reply
#2
You could use a defaultdict with its factory set as a list
Use enumerate on your for loop to get the index and each letter per iteration.
Inside the for loop using the defaultdict append the index to the letter key.
Reply
#3
foo = 'GTCCGTTTAGAAGGTTT'
for idx, item in enumerate(foo):
    print(f'index:{idx}, item:{item}')
Output:
index:0, item:G index:1, item:T index:2, item:C index:3, item:C index:4, item:G index:5, item:T index:6, item:T index:7, item:T index:8, item:A index:9, item:G index:10, item:A index:11, item:A index:12, item:G index:13, item:G index:14, item:T index:15, item:T index:16, item:T
enumerate() takes optional start argument with default value=0
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020