I have a set s and a list of set l as below.
s = {1,2,3,4}
l = [{1}, {1,2,3}, {3}]
The output should be
out = [{1}, {1,2,3}, {3}]
I am using the following code to accomplish it. But I was hoping there would be a faster way? Perhaps some sort of broadcasting?
out = [i.intersection(s) for i in l]
EDIT
List l can be as long as 1000 elements long.
My end objective is to create a matrix which has the length of elements of the pairwise intersection of elements of l. So s is an element of l.
out_matrix = list()
for s in l:
out_matrix.append([len(i.intersection(s)) for i in l])
lthat you are concerned about performance?x in ywhenyis a set is O(1), so you currently have an O(n) operation.