[Python] Remove equal characters from two python strings

Discussão em 'Python' iniciado por Stack, Janeiro 23, 2025.

  1. Stack

    Stack Membro Participativo

    I am writing a Python code to remove equal same characters from two strings which lies on the same indices. For example remove_same('ABCDE', 'ACBDE') should make both arguments as BC and CB. I know that string is immutable here so I have converted them to list. I am getting an out of index error.

    def remove_same(l_string, r_string):
    l_list = list(l_string)
    r_list = list(r_string)
    i = 0
    while i != len(l_list):
    print(f'in {i} length is {len(l_list)}')
    while l_list == r_list:
    l_list.pop(i)
    r_list.pop(i)
    if i == len(l_list) - 1:
    break
    if i != len(l_list):
    i += 1

    return l_list[0] == r_list[0]

    Continue reading...

Compartilhe esta Página