Perseus_Lynx
Perseus_Lynx

Reputation: 31

How can I find the first difference between two strings in Python?

Let's say I have:

a = 'abcde'
b = 'ab34e'

How can I get printed out that the difference in a is c,d and in b is 3,4? Also would it be possible to also the get the index of those?

I know I should use difflib, but the furthest I've got is with this overly complex code from this website: https://towardsdatascience.com/side-by-side-comparison-of-strings-in-python-b9491ac858

and it doesn't even do what I want to do. Thank you in advance.

Upvotes: 1

Views: 1479

Answers (3)

Ovski
Ovski

Reputation: 662

I would use zip in the for loop to handle size difference in the two lists at the same time. Otherwise you could run into an index error.

a = "abcde3asdfat"
b = "ab2de2asdfqT"

count = 0
for string_a, string_b in zip(a, b):
    if string_a not in string_b:
        print(f'{string_a} mismatched {string_b} at index {count}')
    count += 1

Upvotes: 1

DataFramed
DataFramed

Reputation: 1631

Simple Answer

For a -b:

a_diff  = [i for i in set(a) if i not in set(b) ]

for b -a:

b_diff = [i for i in set(b) if i not in set(a) ]

Combine the two results to get aggregate diff:

a_diff + b_diff

My output:

enter image description here

Upvotes: 1

JarroVGIT
JarroVGIT

Reputation: 5279

Something like this?

a = "abcde"
b = "ab2de"

for index, char in enumerate(a):
    if not b[index] == char:
        print(f"First mismatch on index {index}: {char} is not {b[index]}")

Would print: First mismatch on index 2: c is not 2

Another possibility would be to create a list of 3-way-tuples, where the first element would be the index, the second element would be the char in a and the third element would be the char in b:

a = "abcde"
b = "ab23e"
print([(index, char, b[index]) for index, char in enumerate(a) if not b[index] == char])

Would result in [(2, 'c', '2'), (3, 'd', '3')]

Upvotes: 2

Related Questions