'Extracting tuples in a set
I have this sample of set: {(1, 1, 5), (1, 2, 4), (1, 3, 3), (1, 4, 2), (1, 5, 1), (2, 1, 4), (2, 2, 3)}.
Now i need to remove the tuples that contains some specific numbers. For example: From this set, remove the tuples that contain [4,5], so the output should be this: {(1, 3, 3), (2, 2, 3)} for the sample.
I used this code to find all combinations but i'm stucked now.
def compositions(k, n):
if n==0:
return []
if k == 1:
return [(n,)]
comp = []
for i in range(n + 1):
for t in compositions(k - 1, n - i):
if i>0:
comp.append((i,) + t)
return set(comp)
compositions(3, 7)
How can i do this?
Thank you very much in advance.
Solution 1:[1]
def extract(array, filter):
res = []
for i in array:
flag = 1
for j in filter:
if j in i:
flag = 0
break
if flag:
res.append(i)
return res
print(
extract(
[(1, 1, 5), (1, 2, 4), (1, 3, 3), (1, 4, 2), (1, 5, 1), (2, 1, 4), (2, 2, 3)],
[4,5]
)
)
You can try something like this.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | king juno |
