Given a set strings of containing numbers, how can I find those strings that are the superset. For example if strings '139 24' and '139 277 24' appear then I want to keep '139 277 24' as '139 24' can be found inside it. Also these numbers may appear in any order inside a string.
'24'
'277'
'277 24'
'139 24'
'139 277 24'
'139 277'
'139'
'136 24'
'136 277 24'
'136 277'
'136'
'136 139 24'
'136 139 277 24'
'136 139 277'
'136 139'
'246'
The result for the above data is given below.
'136 139 277 24'
'246'
Edit: I am splitting each string and putting individual numbers in a set and then comparing this through the sets created from the whole list. I can find a solution using this approach but I think there should be some other elegant way to perform the same.
I was trying the following code and felt that it is becoming unnecessarily complex.
#First create a set of tuples
allSeqsTuple = set()
for seq in allSeqs: #allSeqs store the sequences described above
x = seq.split()
allSeqsTuple.add(tuple(x))
#For each 'allSeqs', find if all the items in that seq that is in 'allSeqsTuple'.
for line in allSeqs:
x = set(line.split())
result = findContainment(x, allSeqsTuple)
......
......
def findContainment(x, allSeqsTuple):
contained = False
for y in allSeqsTuple:
cntnd = bool(x-set(y))
if (cntnd):
contained = True
continue
else:
break
return contained
set.issuperset(other)
method which can also be written as a logical expressionset >= other
.