Home > Software design >  Remove entries in a python set based on condition
Remove entries in a python set based on condition

Time:12-16

I used scipy.spatial.KDTree.query_pairs() which returned a python set of tuples. Let's say, this is the output:

set1 = {(2, 3), (4, 5), (1, 6), (6, 7), (3, 8), (6, 8)}

Next, I want to erase all the tuples in the set which do not fulfill the condition

arr = [6, 7]
tuple[0] in arr or tuple[1] in arr

What would be the most elegant/fast/pythonic way? Should I maybe convert to a numpy array?

CodePudding user response:

You need to iterate over and check every tuple in set1, you can do that using a set comprehension, and any():

>>> set1 = {(2, 3), (4, 5), (1, 6), (6, 7), (3, 8), (6, 8)}
>>> arr = [6, 7]
>>> set2 = set(arr)  # convert to set for O(1) lookup time
>>> set3 = {t for t in set1 if any(x in set2 for x in t)}
>>> set3
{(6, 7), (6, 8), (1, 6)}

CodePudding user response:

One way to approach this would be to iterate through the set and create an array for the elements which fulfill the conditions and then convert the array to a set.

This can be done with a one-line solution such as:

set2 = set([a for a in set1 if a[0] not in key and a[1] not in key])

CodePudding user response:

Hope this is what you are looking for

filtered = [x,y for x,y in set1 if (x,y) == (6,7)]

or try using lambda for complex filters.

CodePudding user response:

Without converting your data to numpy arrays, this is how I would address this problem:

set1 = {(2, 3), (4, 5), (1, 6), (6, 7), (3, 8), (6, 8)}
arr = [6, 7]

filtered = list(filter(lambda xy: bool(set(xy) & set(arr)), set1))

If the length of the elements within set1 grows considerably and you decide to use numpy, then I would just change the sets intersection for numpy's intersect1d

If instead the length of set1 grows, then I suggest you take a look on this other StackOverflow post to see how you can filter a numpy array.

  • Related