0

I want to make a function that will remove '-' from two sequences of char if both contain it. This is my code.

def normalized(seq1, seq2):
    x = ''
    y = ''
    for a, b in zip(seq1, seq2):
    if a != '-' and b != '-':
        print a,b, 'add'
        x += a
        y += b
    else:
        print a, b, 'remove'
return x,y

x = 'ab--dfd--df'
y = 'rt-bfdsu-vf'

print normalized(x, y)

and this is the result.

a r add
b t add
- - remove
- b remove
d f add
f d add
d s add
**- u remove**
- - remove
d v add
f f add
('abdfddf', 'rtfdsvf')

You can see that - and u should not be removed. What's wrong with my code?

2
  • 1
    But - b should be removed? Commented May 30, 2013 at 13:36
  • 3
    And your indentation is broken, can you fix it please? Commented May 30, 2013 at 13:36

5 Answers 5

4

You want to use or, not and ...


Another clever way that you could do this is to use operator chaining:

if a == b == '-':
   print a,b, 'remove'
else:
   print a,b, 'add'
   x += a
   y += b

This is extremely succint and clear that you want to remove the dashes only if they appear in both strings.

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

4 Comments

@GrijeshChauhan it is equivalent to (a ==b and b == '-')
It's called operator chaining as mgilson wrote and is a feature of python ;-) you can also do 5 < x < 10 instead of x > 5 and x < 10 etc.
It's documented here
@AshwiniChaudhary, Septi, mgilsom ..nice feature.. it don't happen in our C :)
3

If you only want to remove if both are -, then test for that:

if not (a == '-' and b == '-'):

which can be shortened to:

if not (a == b == '-'):

or use or instead of and to remove the not:

if a != '-' or b != '-':

but that is not as readable.

Perhaps no more readable, but the list comprehension would be:

def normalized(seq1, seq2):
    return [''.join(v) for v in zip(*[(a, b) 
                for a, b in zip(seq1, seq2) if not (a == b == '-')])]

or using map(), sufficient for Python 2:

def normalized(seq1, seq2):
    return map(''.join, zip(*[(a, b) for a, b in zip(seq1, seq2) if not (a == b == '-')]))

7 Comments

de Morgan's law simplifies this in @mgilson answer.
or if not a == b == '-':
this one if not (a == b == '-'): works. I just miss the logic here :p Thanks for your reduced code :D
@user2435611: The part in the parenthesis is only True if both a and b are equal and both are equal to -. The not just turns that around to when a is not equal to b, or if they are, at least not equal to -.
Yes, your code is just what I want to do and you reduce it into 2 lines only :D Thanks a lot
|
1

The condition should be not (a =='-' and b == '-'):

def normalized(seq1, seq2):
    x = ''
    y = ''
    for a, b in zip(seq1, seq2):
        if not (a =='-' and b == '-'):  # you need `not` here
            print a,b, 'add'
            x += a
            y += b
        else:
            print a, b, 'remove'
    return x,y

x = 'ab--dfd--df'
y = 'rt-bfdsu-vf'

print normalized(x, y)

Comments

0

According to your code, - u shoud be removed.

In fact,

a != '-' and b != '-' is False

the first part is false and the second is true. False and True is False. (Boolean Algebra 101, see http://en.wikipedia.org/wiki/Boolean_algebra#Basic_operations for details)

Comments

0

You are currently asking for the program to match when a and b are not "-". This means that unless both are not equal to "-" then it will go to your else. The code you want is:

def normalized(seq1, seq2):
    x = ''
    y = ''
    for a, b in zip(seq1, seq2):
        if a == '-' and b == '-':
            print a,b, 'remove'
        else:
            print a, b, 'add'
            x += a
            y += b
    return x,y

x = 'ab--dfd--df'
y = 'rt-bfdsu-vf'

print normalized(x, y)

2 Comments

Why are you adding while printing 'remove'? :-)
Oops, that was a horribly hacked together bit of code but yeah the addition goes on the else. I'll edit that now.

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.