In Python, How can one subtract two non-unique, unordered lists? Say we have a = [0,1,2,1,0]
and b = [0, 1, 1]
I'd like to do something like c = a - b
and have c
be [2, 0]
or [0, 2]
order doesn't matter to me. This should throw an exception if a does not contain all elements in b.
Note this is different from sets! I'm not interested in finding the difference of the sets of elements in a and b, I'm interested in the difference between the actual collections of elements in a and b.
I can do this with a for loop, looking up the first element of b in a and then removing the element from b and from a, etc. But this doesn't appeal to me, it would be very inefficient (order of O(n^2)
time) while it should be no problem to do this in O(n log n)
time.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…