在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)
這些方法都可以根據具體情況選擇使用,根據數據規模和性能要求選擇最適合的方法。