-1

I would like to program something in python 3 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?

BiswajitPaloi
  • 586
  • 4
  • 16
Ines1709
  • 19
  • 3
  • 5
    Your code is not a [mre] because `i` is not defined. – Random Davis Nov 29 '21 at 17:35
  • 2
    Does this answer your question? [Best way to replace multiple characters in a string?](https://stackoverflow.com/questions/3411771/best-way-to-replace-multiple-characters-in-a-string) – Tomerikoo Nov 29 '21 at 17:36
  • 2
    You never define, nor update `i` so how does this even run? Anyway, why do you even need to iterate over `seq`? Just iterate over `check` and `replace` each one of them. [`replace`](https://docs.python.org/3/library/stdtypes.html#str.replace) replaces ***all*** ocurrences anyway (just like you don't loop with `\n` and `' '`) – Tomerikoo Nov 29 '21 at 17:40

3 Answers3

0

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
Erik McKelvey
  • 1,650
  • 1
  • 12
  • 23
0

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'
j__carlson
  • 1,346
  • 3
  • 12
  • 20
0

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.

user1717828
  • 7,122
  • 8
  • 34
  • 59