comparing tuples

B

Baba

level: beginners

I was trying to write simple code that compares 2 tuples and returns
any element in the second tuple that is not in the first tuple.

def tuples(t1, t2):
result = []
for b in t2:
for a in t1:
if b == a:
break
else:
result=result+[b,]
return result

print tuples([0,5,6], [0,5,6,3,7])


the code works but i was surprised by the following: my understanding
was that an ELSE clause is part of an IF statement. Therefore it comes
at the same indentation as the IF statement.

However the above example only works if the ELSE clause is positioned
under the second FOR loop. As if it was an ELSE clause without an IF
statement....!?

Why/How does this work?

tnx
Baba
 
T

Tim Chase

level: beginners

I was trying to write simple code that compares 2 tuples and returns
any element in the second tuple that is not in the first tuple.

def tuples(t1, t2):
result = []
for b in t2:
for a in t1:
if b == a:
break
else:
result=result+[b,]
return result

print tuples([0,5,6], [0,5,6,3,7])


the code works but i was surprised by the following: my understanding
was that an ELSE clause is part of an IF statement. Therefore it comes
at the same indentation as the IF statement.

The ELSE clause can be used either with an IF (as you know) or
with a FOR loop, which is interpreted as "if this loop reached
the end naturally instead of exiting via a BREAK statement,
execute this block of code".

If you reach the end of t1 without having found a value (and then
issuing a "break"), then the current value of t2 (b) should be
appended to the result.

That said, unless order matters, I'd just use sets:

def tuples(t1, t2):
return list(set(t2)-set(t1))

which should have better performance characteristics for large
inputs.

-tkc
 
B

Baba

level: beginners
I was trying to write simple code that compares 2 tuples and returns
any element in the second tuple that is not in the first tuple.
def tuples(t1, t2):
     result = []
     for b in t2:
         for a in t1:
             if b == a:
                 break
         else:
             result=result+[b,]
     return result
print tuples([0,5,6], [0,5,6,3,7])
the code works but i was surprised by the following: my understanding
was that an ELSE clause is part of an IF statement. Therefore it comes
at the same indentation as the IF statement.

The ELSE clause can be used either with an IF (as you know) or
with a FOR loop, which is interpreted as "if this loop reached
the end naturally instead of exiting via a BREAK statement,
execute this block of code".

If you reach the end of t1 without having found a value (and then
issuing a "break"), then the current value of t2 (b) should be
appended to the result.

That said, unless order matters, I'd just use sets:

   def tuples(t1, t2):
     return list(set(t2)-set(t1))

which should have better performance characteristics for large
inputs.

-tkc

Thanks Tim!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,781
Messages
2,569,619
Members
45,316
Latest member
naturesElixirCBDGummies

Latest Threads

Top