I have two lists like the following:
A = [[1, 2, 3], [1, 2, 4], [4, 5, 6]]
and
B = [[1, 2, 3], [1, 2, 6], [4, 5, 6], [4, 3, 6]]
And I wish to calculate the difference, which is equal to the following:
A - B =[[1, 2, 4]]
In other words, I want to treat A and B as a set of lists (all of the sample size, in this example it is 3) and find the difference (ie, remove all lists in B, which are also in A and return the rest.).
Is there a faster way than using multiple for loops for this?
Simple list comprehension will do the trick:
[a for a in A if a not in B]
output:
[[1, 2, 4]]
If you convert the second list to a set first, then membership tests are asymptotically faster; the downside is you have to convert the rows to tuples so that they can be in a set. (Consider having the rows as tuples instead of lists in the first place.)
def list_of_lists_subtract(a, b):
b_set = {tuple(row) for row in b}
return [row for row in a if tuple(row) not in b_set]
Note that "asymptotically faster" only means this should be faster for large inputs; the simpler version will likely be faster for small inputs. If performance is critical then it's up to you to benchmark the alternatives on realistic data.
You can try this.
# (Untested)
A = [[1, 2, 3], [1, 2, 4], [4, 5, 6]]
B = [[1, 2, 3], [1, 2, 6], [4, 5, 6], [4, 3, 6]]
set_A = set([tuple(item) for item in A])
set_B = set([tuple(item) for item in B])
difference_set = set_A - set_B
difference_list = [list(item) for item in sorted(difference_set)]
print(difference_list)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.