您正在寻找MATLAB的
setdiff –
setdiff(lst1,lst2)
样品运行 –
>> lst1={'a','b','c'};
>> lst2={'c','d','e'};
>> setdiff(lst1,lst2)
ans =
'a' 'b'
用Python运行验证 –
In [161]: lst1=['a','b','c']
...: lst2=['c','d','e']
...: lst3=[]
...: for i in lst1:
...: if i not in lst2:
...: lst3.append(i)
...:
In [162]: lst3
Out[162]: ['a', 'b']
实际上,你在Python的NumPy module中也有setdiff,如numpy.setdiff1d.与它的等效实现将是 –
In [166]: import numpy as np
In [167]: np.setdiff1d(lst1,lst2) # Output as an array
Out[167]:
array(['a', 'b'],
dtype='|S1')
In [168]: np.setdiff1d(lst1,lst2).tolist() # Output as list
Out[168]: ['a', 'b']