I have a huge list of tuples :
ijs = [(0,1),(0,2),(0,3), (3,2)...]
For a given value v
, I want to get only the (i,j)
pairs that have either i=v
or j=v
(from all possible (i,j) pairs stored in ijs
).
E.g. for v=0
and given ijs = [(0,1),(0,2),(0,3), (3,2)]
, then I should get back only_current = [(0,1),(0,2),(0,3)]
Example:
Please ignore the first 3 lines where I built a list ijs
having tuples inside.
import numpy as np
# IGNORE THIS PART UNTIL THE MAIN LOOP
N= 1000
indices_upper_triangle = np.triu_indices(N, k = 1) # k=1 above diagonal
i,j = indices_upper_triangle
ijs = [(i,j) for i,j in zip(i,j)] # create (i,j) positions
# MAIN LOOP HERE
# Main loop
all_neig_results = list()
for v in range(N): # for each point
# from all possible (i,j) pairs, get only (i,j) pairs that have either i=v or j=v
only_current = [item for item in ijs if v in item]
all_neig_results.append(only_current)
The list comprehension in the loop is super slow.
%timeit [item for item in ijs if v in item]
15.9 s ± 361 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
If I remove the check argument if v in item
:
%timeit [item for item in ijs]
1.28 s ± 90.6 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
How can I optimize [item for item in ijs if v in item]
?