1

Lets suppose I have these two strings:

IAMASTRIPETHA-IWANTTOIGN-RE

IAMA-TRIPETHATIWA-TTOIGNORE

If in this case I would ignore the positions of the '-', those two strings are the same. How can I accomplish this in Python 2.7?

IAMASTRIPETHA-IWANTTOIGN-RE

IAMA-TRIPETHATIWA-TTOALGORE

The above example is not similar when ignoring the '-'; hence I don't care.

Hope someone can help me out ;)

PS: Apologies for not mentioning this but it is not required for strings to have an equal length!

8
  • 2
    str1.replace('-','') == str2.replace('-','') Commented Aug 20, 2015 at 11:55
  • 4
    @MarounMaroun thats not what OP wants Commented Aug 20, 2015 at 11:56
  • @vks Are you sure? That's what I understood.. please correct me if I didn't get him. Commented Aug 20, 2015 at 11:57
  • @MarounMaroun try to run it and see. Commented Aug 20, 2015 at 11:57
  • Now I got you.. thanks. Commented Aug 20, 2015 at 11:58

3 Answers 3

4
a = "IAMASTRIPETHA-IWANTTOIGN-RE"
b = "IAMA-TRIPETHATIWA-TTOIGNORE"

all(x==y or x=="-" or y=="-" for x, y in zip(a, b))
>> True
Sign up to request clarification or add additional context in comments.

10 Comments

This answer is nice and pythonic
@AnandSKumar Well seen. I added a condition on the string length to fix that.
@AnandSKumar Your interpretation is what's wrong. There is nothing in the problem specification that states the strings have to be the same length in order to be considered a match. Only that corresponding non-hyphen characters are the same.
Thanks for your comment - I'm going to see if it works in my more complicated cases. I indeed don't mind whether the strings are from different lengths.
Yes, but given that its not clear from the requirements what needs to be done for cases where the strings are not of equal length, it is better to give a solution that works for both cases, than one that only works for one case. Especially since its not that hard to come up with a solution that works for both cases (Not at all hard) . And he has already given that solution, so not sure what you are getting so worked up over.
|
1

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)))

Comments

0

I won't write for you a full solution, but will guide you.

You need to get the indexes of the "-" in both strings, and replace by the empty string in each correspondingly.

In order to find the positions of "-" in a given string, you can have:

get_indexes(st):
     return [m.start() for m in re.finditer('-', st)]

Now you should replace these indexes in the other string by the empty string, and compare them.

This solution is robust and doesn't assume anything about length.

1 Comment

I guess it's robust, but it does take an extra pass over each string. And IME regex tends to be slower than the built-in str methods for simple tasks like this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.