Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check one array's elements contains in another array

**

labels = ['UNREAD', 'CATEGORY_PERSONAL', 'INBOX']
incomingLabels = ['UNREAD','IMPORTANT' 'CATEGORY_PERSONAL', 'INBOX']

**

labels array is static array. How do I check incoming array contains all elemnts of labels array.

My attempts

intersectionOfTwoArrays = list(set(incomingLabels) & set(labels))
if np.array_equal(labels, intersectionOfTwoArrays): 
   //Do somthing 

that attempt not succed because intersectionOfTwoArrays's not ordered same as labels array

Can anyone help me on that?

like image 656
Januka samaranyake Avatar asked Oct 25 '25 04:10

Januka samaranyake


1 Answers

convert both list into set before doing array_equal to avoid order issue

labels = ['UNREAD', 'CATEGORY_PERSONAL', 'INBOX']
incomingLabels = ['UNREAD','IMPORTANT', 'CATEGORY_PERSONAL', 'INBOX']
intersectionOfTwoArrays = list(set(incomingLabels) & set(labels))

if np.array_equal(set(labels), set(intersectionOfTwoArrays)): 
    # Do somthing 
    print "match"

alternatively, you could using set method issubset

labels = ['UNREAD', 'CATEGORY_PERSONAL', 'INBOX']
incomingLabels = ['UNREAD','IMPORTANT', 'CATEGORY_PERSONAL', 'INBOX']

if set(labels).issubset(set(incomingLabels)):
    # issubset true, do something
    print "match"
like image 186
Skycc Avatar answered Oct 26 '25 23:10

Skycc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!