在Python中,可以使用多种方法来匹配两组数据。
list1 = [1, 2, 3, 4, 5]
list2 = [3, 4, 5, 6, 7]
for item in list1:
if item in list2:
print(item, "is present in both lists")
set1 = set([1, 2, 3, 4, 5])
set2 = set([3, 4, 5, 6, 7])
common_elements = set1.intersection(set2)
print(common_elements)
list1 = [1, 2, 3, 4, 5]
list2 = [3, 4, 5, 6, 7]
common_elements = [item for item in list1 if item in list2]
print(common_elements)
import numpy as np
array1 = np.array([1, 2, 3, 4, 5])
array2 = np.array([3, 4, 5, 6, 7])
common_elements = np.intersect1d(array1, array2)
print(common_elements)
这些方法都可以根据具体情况选择使用,根据数据规模和性能要求选择最适合的方法。