I have 3 lists.
A_set = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Q_act = [2, 3]
dur = [0, 4, 5, 2, 1, 3, 4, 8, 2, 3]
All lists are integers.
What I am trying to do is to compare Q_act
with A_set
then obtain the indices of the numbers that match from A_set
.
(Example:
Q_act
has the elements [2,3]
it is located in indices [1,2]
from A_set
)
Afterwards, I will use those indices to obtain the corresponding value in dur
and store this in a list called p_dur_Q_act
.
(Example: using the result from the previous example, [1,2]
The values in the dur
list corresponding to the indices [1,2]
should be stored in another list called p_dur_Q_act
i.e. [4,5]
should be the values stored in the list p_dur_Q_act
)
So, how do I get the index of the common integer element (which is [1,2]
) from two separate lists and plug it to another list?
So far here are the code(s) I used:
This one, I wrote because it returns the index. But not [4,5]
.
p_Q = set(Q_act).intersection(A_set)
p_dur_Q_act = [i + 1 for i, x in enumerate(p_Q)]
print(p_dur_Q_act)
I also tried this but I receive an error TypeError: argument of type 'int' is not iterable
p_dur_Q_act = [i + 1 for i, x in enumerate(Q_act) if any(elem in x for elem in A_set)]
print(p_dur_Q_act)