I have two lists (A and B) with some common elements in between them. I want to create a new list (C) with all the common elements from A that are also in B, as well have the index of where the elements was originally in A.
The two lists are as fallows,
A=[[[a, b], [c, d]],
[[e, f], [f, t]],
[[a, b], [c, d]],
[[g, h], [t, r]]]
B=[[[a, b], [c, d]],
[[g, h], [t, r]]]
The code Im using to find the common elements are as fallows,
C=[[ ]]
for i in range(len(A)):
if A[i] in B:
C[0].append(A[i])
The output is perfect, except I'd like to add in the index of the elements in C from where there come from in A (even if there are duplicates), possible besides each elements or even in a new list, although Im not sure the code that would do that, so any help is appreciated.
You can replace this line of code:
C[0].append(A[i])
with:
C[0].append([A[i], i])
which forms C
with each element in the format [element, index]
.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments