简体   繁体   English

python 字符串替换使用 for 循环与 if else

[英]python string replace using for loop with if else

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 combinely and again search in old_list and so on.我是 python 的新手,试图用 if else 条件替换使用 for 循环的字符串,我有一个字符串并希望以这样的方式替换该字符串的某些字符,即它应该获取/选择字符串的第一个字符并在其中搜索它们old_list 如果字符匹配,则应将该字符替换为 new_list 的字符,如果该字符不匹配,则应将字符串中的该字符(上一个)和下一个字符一起考虑,并组合搜索它们,然后再次在 old_list 中搜索,依此类推上。

it should replace in this oder (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,它应该在这个顺序中替换(从字符串中选择字符)= 010,101,010,010,100,101,00,00,011,1101,011,00,101,010,00,011,1111,1110,00,00,00,010,101,010,替换值 = 1001,00010100, ,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以上述字符串为例,如果我们执行该操作,该字符串将变为最终字符串或结果字符串 = 10010000000010001111100111111111100101010101110011111001000011111001010010110101011111111111100001001

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 = 0
for i in range((old), 0):
    if i == old:
        my_str = my_str.replace(old[i],new[i], 0)
    else:
        i = i + 1

print(my_str)

as result, string become = 10010000000010001111100111111111100101010101110011111001000011111001010010110101011111111111100001001结果,字符串变为 = 10010000000010001111100111111111100101010101110011111001000011111001010010110101011111111111100001001

new = ['a ','local ','is ']
my_str = 'anindianaregreat'
old = ['an','indian','are']

for i, string in enumerate(old):
    my_str = my_str.replace(string, new[i], 1)

print(my_str)

Your usage of range is incorrect.您对range的使用不正确。

range goes from lower (inclusive) to higher (exclusive) or simply 0 to higher (exclusive) rangelower (包含)到higher (不包含)或简单地从 0 到higher (不包含)

Your i == old condition is incorrect as well.您的i == old条件也不正确。 ( i is an integer, while old is a list). i是 integer,而old是一个列表)。 Also what is it supposed to do?还有它应该做什么?

You can simply do:你可以简单地做:

for old_str, new_str in zip(old, new):
    my_str = my_str.replace(old_str, new_str, 1)

https://docs.python.org/3/library/stdtypes.html#str.replace You can provide an argument to replace to specify how many occurrences to replace. https://docs.python.org/3/library/stdtypes.html#str.replace您可以提供要替换的参数以指定要替换的次数。

No conditional is required since if old_str is absent, nothing will be replaced anyway.不需要条件,因为如果old_str不存在,则无论如何都不会替换任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM