user2435611
user2435611

Reputation: 1203

How to use and operator in python?

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?

Upvotes: 0

Views: 436

Answers (5)

ydaetskcoR
ydaetskcoR

Reputation: 56937

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)

Upvotes: 0

mgilson
mgilson

Reputation: 310049

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.

Upvotes: 4

Martijn Pieters
Martijn Pieters

Reputation: 1123410

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 == '-')]))

Upvotes: 3

njzk2
njzk2

Reputation: 39406

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)

Upvotes: 0

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 251051

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)

Upvotes: 1

Related Questions