Python Forum

Full Version: keep only one tuple between 2
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi

I've 2 tuples (coming from numpy.where) which one is necessary empty:
a = tuple1
b = tuple2

In one sentence, I'm wondering how can I keep the one that is not empty?

For example, I had a look to 'numpy.any' that give me back 'True' or 'False', but I'm not able to go further.

Thanks for your help

Paul
There is lot of ambiguity here but tuples are compared lexicographically and booleans 'and' and 'or' return one of their operands. So one can do:

>>> a, b = (), (0,)                                                                       
>>> a < b and b or a                                                                      
(0,)
>>> a, b = (0,), ()                                                                       
>>> a < b and b or a                                                                      
(0,)
Hi perfringo

Thanks for your answer; based on your example, I did some tests to understand how it works and it helped me a lot

Paul

import numpy as np

# my tupples
a = ( 0, 15, 49, 100, 1568)
b = ()

# 1rst (splitted) solution
result  = (a < b and b) or a
c  = a < b
d = a < b and b
e = a < b or a

# another one
f = (np.any(a))*a or (np.any(b)*b) # being deprecated
f = ((np.any(a)).astype(int))*a + ((np.any(b)).astype(int))*b