10 examples of 'python find intersection of two lists' in Python

Every line of 'python find intersection of two lists' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
226def getPairIntersection(set1,set2):
227 acc = []
228 # print set1,'+++',set2
229 for el1,el2 in zip(set1,set2):
230 temp2 = []
231 temp2.append(tuple([val for val in el1[0] if val in el2[0]]))
232 for el1x,el2x in zip(el1[1],el2[1]):
233 temp2.append(([val for val in el1x if val in el2x],))
234 acc.append(tuple(temp2))
235 return acc
2def intersect(self, nums1, nums2):
3 """
4 :type nums1: List[int]
5 :type nums2: List[int]
6 :rtype: List[int]
7 """
8 ans = []
9 nums1.sort()
10 nums2.sort()
11 i = j = 0
12 while i < len(nums1) and j < len(nums2):
13 if nums1[i] < nums2[j]:
14 i += 1
15 elif nums1[i] > nums2[j]:
16 j += 1
17 else:
18 ans.append(nums1[i])
19 i += 1
20 j += 1
21
22 return ans
16def intersect(*lists):
17 return list(reduce(set.intersection, (set(l) for l in lists)))
94def intersect(self, nums1, nums2):
95 """
96 :type nums1: List[int]
97 :type nums2: List[int]
98 :rtype: List[int]
99 """
100 nums1.sort(), nums2.sort() # Make sure it is sorted, doesn't count in time.
101
102 res = []
103
104 it1, it2 = 0, 0
105 while it1 < len(nums1) and it2 < len(nums2):
106 if nums1[it1] < nums2[it2]:
107 it1 += 1
108 elif nums1[it1] > nums2[it2]:
109 it2 += 1
110 else:
111 res += nums1[it1],
112 it1 += 1
113 it2 += 1
114
115 return res
30def intersect(first_list, second_list):
31
32 return [x for x in first_list if x in second_list]
5def intersection(self, nums1, nums2):
6 """
7 :type nums1: List[int]
8 :type nums2: List[int]
9 :rtype: List[int]
10 """
11 if len(nums1) > len(nums2):
12 return self.intersection(nums2, nums1)
13
14 lookup = set()
15 for i in nums1:
16 lookup.add(i)
17
18 res = []
19 for i in nums2:
20 if i in lookup:
21 res += i,
22 lookup.discard(i)
23
24 return res
61def union(list1, list2):
62 if list1 is None:
63 list1 = {}
64 if list2 is None:
65 list2 = {}
66
67 # (Order matters slightly so that has_key is called fewer times)
68 if len(list1) < len(list2):
69 smaller = list1
70 bigger = list2
71 else:
72 smaller = list2
73 bigger = list1
74
75 if isinstance(bigger, dict):
76 union_dict = bigger
77 else:
78 union_dict = {}
79 for e in bigger:
80 union_dict[e] = bigger[e]
81 for e in smaller:
82 union_dict[e] = smaller[e]
83 return union_dict
142def Intersection(A, B):
143 """
144 Returns the pixel count corresponding to the intersection
145 between A and B.
146 """
147 C = A + B
148 C[C != 2] = 0
149 C[C == 2] = 1
150 return C
506def intersection_update(self, other):
507 """Update self to include only the intersection with other."""
508 other = set(other)
509 indices_to_delete = set()
510 for i, elem in enumerate(self):
511 if elem not in other:
512 indices_to_delete.add(i)
513 if indices_to_delete:
514 self._delete_values_by_index(indices_to_delete)
57def find(self,i):
58 if self.findSet[i]!=i:
59 n = self.find(self.findSet[i])
60 self.findSet[i] = n
61 return self.findSet[i]

Related snippets