1. 使用集合的交集方法:
set1 = {1, 2, 3, 4, 5}set2 = {4, 5, 6, 7, 8}intersection = set1.intersection(set2)print("交集结果:", intersection)
2. 使用列表推导式:
b1 = [1, 2, 3]b2 = [2, 3, 4]intersection = [val for val in b1 if val in b2]print(intersection)
3. 使用集合操作符 `&`:

b1 = [1, 2, 3]b2 = [2, 3, 4]intersection = list(set(b1) & set(b2))print(intersection)
4. 使用 `filter()` 函数和 `lambda` 表达式:
b1 = [1, 2, 3]b2 = [2, 3, 4]intersection = list(filter(lambda x: x in b2, b1))print(intersection)
5. 对于嵌套类型的列表,可以使用类似的方法:
b1 = [1, 2, 3]b2 = [[2, 4], [3, 5]]intersection = [val for sublist in b2 for val in sublist if val in b1]print(intersection)
以上方法都可以用来求两个集合的交集。选择哪种方法取决于具体的应用场景和个人偏好
