Click here to Skip to main content
15,897,093 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am new with python, trying to replace string using for loop with if else condition,
I have a string and want to replace some character of that string in a such way that it should take / pick first character of the string and search them in the old_list if the character match it should replace that character with the character of new_list and if the character does not match it should consider that character (previous) and the next character together of the string and search them combine and again search in old_list and so on.

it should replace in this order (picking the character from string) = 010,101,010,010,100,101,00,00,011,1101,011,00,101,010,00,011,1111,1110,00,00,00,010,101,010,
replacing value = 1001,0000,0000,1000,1111,1001,1111,1111,100,1010101011,100,1111,1001,0000,1111,100,10100101,101010,1111,1111,1111,0000,1001,

with the example of above string if we performed that operation the string will becomes
final or result string = 10010000000010001111100111111111100101010101110011111001000011111001010010110101011111111111100001001

What I have tried:

Python
string = 01010101001010010100000111101011001010100001111111110000000010101010
old_list = ['00','011','010','101','100','1010','1011','1101','1110','1111']
new_list = ['1111','100','0000','1001','1000'1111','0101','1010101011','101010','10100101']


i = []
for i in range(0, len(string)):
    if i == old:
        string = string.replace(old[i], new[i], 1)
    else:
        i = +1

print(string)
print(string)
Posted
Updated 18-Apr-21 23:47pm
v2

1 solution

Your code is just wrong.
- variables old and new do not exist.
- you declare i as an array and then use it as an index on string, old and new at same time.
- replacing the way you do is just wrong.
Python
# next line stores an integer in string
string = 01010101001010010100000111101011001010100001111111110000000010101010
# next line stores a string in string
string = '01010101001010010100000111101011001010100001111111110000000010101010'

My advice: do not replace, build a result string instead.

Think about what you want: match consecutive strings from an array in variable string.
01010101001010010100000111101011001010100001111111110000000010101010
010
   101
      010
         010
            100
               101
                  00
                    00
                      011
                         1101
                             011
                                00
                                  101
                                     010
                                        00
                                          011
                                             1111
                                                 1110
                                                     00
                                                       00
                                                         00
                                                           010
                                                              101
                                                                 010

The index in string must move with steps of 2, 3 or 4 depending on which element of array matched.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900