Python Forum
keep only one tuple between 2
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
keep only one tuple between 2
#1
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
Reply
#2
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,)
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
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
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020