0

I have two strings like s1='fly,dream';s2='dream,fly' I want the s1 equals to s2.

The code I tried is:

def Isequal(m,n):
    s1=m.split(',')    s2=n.split(',')   s1.sort()   s2.sort()
    if s1 == s2:
        print 'Equal'
    else:
        print s1,s2

Note:s1 may be equal to s2. Then

def Isequal(m,n):
s1=m.split(',')
s2=n.split(',')
if s1 == s2.reverse() || s1 == s2:
print 'Equal'
else:
print s1,s2

Is this code right? I there something to improve?

3
  • Is s1 equal to itself or are they only Isequal if the second is the "reverse" of the first? Commented Jul 9, 2013 at 14:01
  • 4
    You need to define what "equals" means in this case. I can think of many different definitions that satisfy the single example you gave. I'm guessing that you want the lists to be equal if they contain the same comma-delimited terms, is that correct? Commented Jul 9, 2013 at 14:05
  • Perhaps I'm being snarky but if you want s1 to equal s2, you can just do s1 = s2. Then they will be equal. (I think you wanted to know if the order of the words, separated by commas, of each string is the inverse of the other.) Commented Jul 9, 2013 at 23:27

3 Answers 3

5

Your code splits the two strings by , (which returns a list) and calls the sort method on the list. Since the two substrings are identical, sorting the list of the substrings results in equal lists. The best way to know what is happening is printing the stuff out. See the results.

>>> s1 = 'fly,dream'
>>> s2 = 'dream,fly'
>>> s1 = s1.split(',')
>>> s1
['fly', 'dream']
>>> s2 = s2.split(',')
>>> s2
['dream', 'fly']
>>> s1.sort()
>>> s1
['dream', 'fly']
>>> s2.sort()
>>> s2
['dream', 'fly']
>>> s1 == s2
True

If you want to check that the two strings consist of the same substrings, use sets, like follows :

>>> varOne = set(s1.split(','))
>>> varTwo = set(s2.split(','))
>>> varOne == varTwo
True

Beware that sets only allow unique items, so fly,dream,fly and dream,dream,fly will result in True here.

Sign up to request clarification or add additional context in comments.

Comments

3

Set would be more elegant here:

def Isequal(m, n):
    s1 = set(m.split(','))
    s2 = set(n.split(','))
    if s1 == s2:
        print 'Equal'
    else:
        print s1, s2

and should be more efficient too.

3 Comments

Depends on whether "dream,fly,dream" should be 'equal' to "dream,fly".
You may be right @tobias_k, so +1 for your comment. Equality isn't defined well in this question. I've assumed that if ordering is irrelevant then multiple occurrences too.
Agreed, but you could extend your answer by your "interpretation" of the equality criterion. ;-)
1

You probably don't want to use sort() to flip a list. The sorting that takes place entirely depends on the string (it varies on the first letter of each string). You can use .reverse to reverse a list:

def Isequal(m,n):
    m = m.split(',')
    m.reverse()
    if m == n.split(','):
        print "Equal"
    else:
        print m, n

If you want to sort the list in place, you can always do .reverse() instead of .sort()


If reversing the list was just an example in your question, and your strings would actually have more items when you split them, you can use sets:

def Isequal(m,n):
    if not set(m.split(',')).symmetric_difference(n.split(',')):
        print "Equal"
    else:
        print m, n

By the way, Sparse is better than dense.. The semi-colons are rather... ugly.

6 Comments

That works for this specific example, but I think he's looking at a more general problem: having two strings composed of keywords separated by commas, how do you say they're the same set? I think putting them in a set is probably more useful, if you don't care about repeats.
@jszakmeister I've added this is to my answer. Is this what you meant?
Yes, that's what I meant. :-)
Alternatively: not set(m.split(',')).symmetric_diffeence(n.split(','))
Doesn't reversed return a reverse iterator?
|

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.