4

**

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?

5
  • What do you mean by not ordered same as labels array? Commented Oct 21, 2016 at 8:56
  • labels = ['UNREAD', 'CATEGORY_PERSONAL', 'INBOX'] intersectionOfTwoArrays = ['UNREAD', 'INBOX','CATEGORY_PERSONAL'] because of that if condition fails Commented Oct 21, 2016 at 8:57
  • I still have no idea what you mean, what does the order have to do with what you are trying to do? Commented Oct 21, 2016 at 8:58
  • check arrays are quals or not Commented Oct 21, 2016 at 8:59
  • You mean set(incomingLabels) >= set(labels)? array_equal does an elementwise comparison. Are you actually using numpy arrays? Commented Oct 21, 2016 at 9:00

1 Answer 1

12

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"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.