Python Forum
keep only one tuple between 2 - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: keep only one tuple between 2 (/thread-22186.html)



keep only one tuple between 2 - paul18fr - Nov-02-2019

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


RE: keep only one tuple between 2 - perfringo - Nov-03-2019

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,)



RE: keep only one tuple between 2 - paul18fr - Nov-03-2019

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