Предположим, есть две строки:
str1 = "15323"
str2 = "12314"
Как найти индексы чисел в обеих строках, которые не находятся в одном и том же порядке на основе одной из строк? Ожидаемое значение:
[3, 4] # the numbers in str1 that are in str2 but not in the exact place of str2: 2 and 3
"15323"
^^
"12314"
^^
Еще один пример, поскольку я не могу это объяснить:
str1 = "3546"
str2 = "1346"
Ожидаемое значение:
[0] # only "3" is in str1 AND str2 and it does not have the same index as the "3" in str2
"3546"
"3" in "3546" appears in str2 and it does not have the same index
"5" in "3546" does not appear in str2
"4" in "3546" appears in str1 but has the same index as the "4" in str2
"6" in "3546" appears in str1 but has the same index as the "6" in str2
Я пробовал код, чтобы найти общие элементы в две строки, но я не могу обернуть голову вокруг дубликатов. Я не хотел бы импортировать какие-либо модули, и спасибо заранее.
result = []
for string in str1:
if string in str2:
if str1.index(string) != str2.index(string):
result.append(str1.index(string))