I have the following list of lists:
db=[['C','A','G','A','A','G','T'],['T','G','A','C','A','G'],['G','A','A','G','T']]
I need to get where each individual symbol (that is to say, ['C', 'A', 'G', 'T']) appears inside each of the sublist. In the end, the result must be a tuple containing the symbol and a list of tuples that contain the index of the sublist where the symbol appears, and a list of indexes representing in which position of such sublist the symbol appears
That is to say:
FinalList=[('C',[(0,[0]),(1,[3])]),
('A',[(0,[1,2,4]),(1,[2,4]),(2,[1,2])]),
('G',[(0,[2,5]),(1,[1,5]),(2,[0,3])]),
('T',[(0,[6]),(1,[0]),(2,[4])])
]
To better explain the result, lets take the first element of FinalList.
('C',[(0,[0]),(1,[3])])
Here, 'C' is the symbol, Because 'C' appears in the db[0] and db[1] but not in db[2], we have [(0,[0]),(1,[3])]. Furthermore, because in db[0], 'C' appears in position 0, we have (0,[0]), and because in db[1] 'C' appears in position 3, we have (1,[3])
The only thing I have managed to do is extract every unique symbol via the following code, although I believe it's inefficient as it combines every list and then makes the result a dictionary, something that would take ages to run if db had more elements
symbols=[]
for i in db:
symbols+=i
symbols=list(dict.fromkeys(symbols))
I now planned to compare each element in symbols with every element in db, then save the index where the symbols are the same, but I think it would be inefficient to use for loops inside for loops. But I am stuck here and don't know how to proceed.