It appears from the comments that you don't care if the string lengths don't match, so we don't need to test lengths, and we can use the built-in zip() rather than importing zip_longest().
s1 = 'IAMASTRIPETHA-IWANTTOIGN-RE'
s2 = 'IAMA-TRIPETHATIWA-TTOIGNORE'
s3 = 'IAMA-TRIPETHATIWA-TTOALGORE'
def ignore_dash_match(s1, s2):
return all(c1 == c2 for c1, c2 in zip(s1, s2) if c1 != '-' and c2 != '-')
print ignore_dash_match(s1, s2), ignore_dash_match(s1, s3)
output
True False
Here's an alternative approach which converts each '-' to a "wildcard" object that compares equal to anything.
s1 = 'IAMASTRIPETHA-IWANTTOIGN-RE'
s2 = 'IAMA-TRIPETHATIWA-TTOIGNORE'
s3 = 'IAMA-TRIPETHATIWA-TTOALGORE'
class Any:
def __eq__(self, other):
return True
def dash_to_Any(s):
return [Any() if c == '-' else c for c in s]
print dash_to_Any(s1) == dash_to_Any(s2), dash_to_Any(s1) == dash_to_Any(s3)
output
True False
You could make that slightly more efficient by using a single instance of Any, rather than creating a fresh one every time. But for a better version of Any please see my answer to Searching for a partial match in a list of tuples.
And of course if you don't care about mismatched lengths you can do
def ignore_dash_match(s1, s2):
return all(c1 == c2 for c1, c2 in zip(dash_to_Any(s1), dash_to_Any(s2)))
str1.replace('-','') == str2.replace('-','')