'How can I make a function to find different characters between two strings
string A will be the string that string B is being compared to, in a function like this: def compare(a,b):
I want this to figure out the characters that are in B that aren't in A, and print the character as well as the index of the characters that are different, I have researched this and can't seem to find a functional solution that works as I want it to, could someone point me in the right direction? note, this is also being done in python.
sample:
compare("aa","bb")
output:
(0,"b",1,"b")
Solution 1:[1]
If they need to be at the same position:
import itertools.zip_longest
def compare(a: str, b: str) -> set[tuple[int,str]]:
diff_set = set()
for i, (ai, bi) in enumerate(itertools.zip_longest(a, b)):
if ai != bi:
diff_set.add( (i, bi) )
return diff_set
What does it do?
- It creates a set (each combination of position and character are only there exactly one time. And I like sets :P.
- We use zip_longest to go through tuples of both strings: "abcd" and "1234" will bring ("a", 1), ("b", 2) .... - wie need this instead of zip because the strings might differ in length
- We're enumerating because we need the index.
- Check if equal and if not, then save it in our set
Edit: I've seen the more pythonic solution above. It's great. So with all:
from itertools import zip_longest
def compare(a: str, b: str):
return {
(i, bi)
for i, (ai, bi) in enumerate(zip_longest(a, b))
if ai != bi
}
Solution 2:[2]
zip the strings, enumerate the pairs and print the ones that differ:
def compare(a, b):
for i, (char_a, char_b) in enumerate(zip(a, b)):
if char_a != char_b:
print(i, char_b)
Solution 3:[3]
You can use itertools.zip_longest, enumerate and filtering in a list comprehension:
from itertools import zip_longest
def compare(A, B):
# tuple of index/char or B for all pairs of chars
return [(i,b) for i,(a,b) in enumerate(zip_longest(A,B, fillvalue='no char!'))
if a!=b] # if the chars are different
Example:
compare("aa","bb")
# [(0, 'b'), (1, 'b')]
compare("abc","aB")
# [(1, 'B'), (2, 'no char!')]
Details on the behavior: zip_longest forms pairs of characters, until one of the two strings is exhausted at which point it fills with the default fillvalue. Then the list comprehension loop over the pairs and keeps only those that are different. enumerate enables to count the position while iterating.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Tomerikoo |
| Solution 3 |
