سؤال

I want to intersect two lists (with NOT), and return the elements of list A that are not present in list B.

example:

>>> a = [1,2,3,4,5]
>>> b = [1,3,5,6]
>>> list(set(a) ????? set(b))
[2, 4]
هل كانت مفيدة؟

المحلول

You are looking for the set difference; the - operator will do that for you:

list(set(a) - set(b))

If you use the set.difference() method the second operand does not need to be a set, it can be any iterable:

list(set(a).difference(b))

Demo:

>>> a = [1,2,3,4,5]
>>> b = [1,3,5,6]
>>> list(set(a).difference(b))
[2, 4]

نصائح أخرى

Something like this?

>>> list(set(a) - set(b))
[2, 4]
a = [1,2,3,4,5]
b = [1,3,5,6]
print list(set(a) - set(b))
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top