I have a list of tuples with each tuple carrying the following information:
(start_position,end_position,list of strings)
A sample list is given as follows:
aList = [(6, 9, ['ataH']),
(4, 9, ['svataH']),
(0, 9, ['vEvasvataH']),
(2, 5, ['vasu', 'vasU']),
(1, 3, ['Eva', 'eva']),
(0, 1, ['vA', 'vE'])]
I need to find all sequences of tuples such that each sequence has to cover all positions from the start_position
to end_position
, in this case from 0
to 9
. In a sequence, say a
, adjacent tuples need to satisfy the constraints that a[i+1][0] - a[i][1] <= 1
.
Summarily, the output should be as follows:
[[(0, 1, ['vA', 'vE']), (2,5,['vasu', 'vasU']), (6, 9, ['ataH']) ],
[(0, 1, ['vA', 'vE']), (1, 3, ['Eva', 'eva']), (4, 9, ['svataH'])],
[(0, 9, ['vEvasvataH'], [7])]]
I have used the following code to achieve the same.
maxVal = max(aList,key=lambda item:item[1])[1]
diffSets = list()
for i,item in enumerate(aList):
if maxVal == item[1]:
_temp = [item]
currStart = item[0]
for j,stuff in enumerate(aList):
if i != j:
if currStart == stuff[1] or currStart == stuff[1]+1:
_temp.append(stuff)
currStart = stuff[0]
diffSets.append(_temp) #the output needs to be reversed to get the sequence in the order as shown above
Is there a more efficient and faster way to achieve the same, say using itertools
?