Home > Software engineering >  Python3: Looping over a string
Python3: Looping over a string

Time:11-30

I would like to program something in python3 and do not understand where my mistake is.

seq = ('1gnadutnpawihv\n  casc341')

check = ('0','1', '2', '3', '4', '5', '6', '7', '8', '9')

while i < len(seq):
    for j in range(len(check)):
        if seq[i] == check[j]:
            seq=seq.replace(seq[i],"")
            

seq=seq.replace("\n","")
seq=seq.replace(" ","")

seq

I want to eliminate the characters "\n", " ", and all numbers from 0 to 9 from the string seq with the replace function. Now I want to iterate over the seq and compare each character with each character of the tuple check and detect the numbers 0 to 9 and replace them afterwards with nothing. The replacement method works for "\n" and " ", but not for the numbers. The output is simply:

'1gnadutnpawihvcasc341'

Why doesn't it work?

CodePudding user response:

The problem was with the outer while loop. Instead of fixing it I removed it because it was redundant. I also removed the \n and spaces in the same loop:

seq = ('1gnadutnpawihv\n  casc341')
check = ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '\n', ' ')

for j in range(len(check)):
    seq = seq.replace(check[j], "")

print(seq)

Output:

gnadutnpawihvcasc

CodePudding user response:

There are better ways to do what you're trying to do, but to answer your question

Why doesn't it work?

the answer becomes clear if you look at seq after the error:

In [1]: seq = ('1gnadutnpawihv\n  casc341')
   ...: 
   ...: check = ('0','1', '2', '3', '4', '5', '6', '7', '8', '9')
   ...: 
   ...: i = 0
   ...: while i < len(seq):
   ...:     for j in range(len(check)):
   ...:         if seq[i] == check[j]:
   ...:             seq=seq.replace(seq[i],"")
   ...: 
   ...:     i  = 1
   ...: 
   ...: seq=seq.replace("\n","")
   ...: seq=seq.replace(" ","")
   ...: 
   ...: seq
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-1-ef8657e9ddaa> in <module>
      6 while i < len(seq):
      7     for j in range(len(check)):
----> 8         if seq[i] == check[j]:
      9             seq=seq.replace(seq[i],"")
     10 

IndexError: string index out of range

In [2]: seq
Out[2]: 'gnadutnpawihv\n  casc'

seq went from 24 characters down to 20. Generally, changing the thing you're iterating over causes things like this to happen in Python. I'm not actually sure how to get the output you posted, but I also had to make some changes (like define what i is) so I'm guessing your code has something else not shown.

CodePudding user response:

There is no need for while or if statements. while is not necessary because for will terminate once it has iterated through the list given. if is not needed because replace() will have no affect if the passed substring is not present in the string. A for loop on its own is enough to produce he desired string.


Code:

seq = ('1gnadutnpawihv\n  casc341')

for x in list(range(10)) [" ", "\n"]:
    seq=seq.replace(f"{x}", "")

Output:

'gnadutnpawihvcasc'
  • Related