Each element in the list L is a tuple of the form (fields, size). For example
L = [ (['A','B'], 5), (['A'], 6), ('C', 1)]
I would like to cull the list so it only contains non-intersecting members, and each member that remains was greater than any other members it may have intersected. So the example list L would be reduced to
L = [ (['A'], 6), ('C', 1)]
Currently I have it implemented like so:
def betterItem(x, y):
return (x != y and
set(x[0]) & set(y[0]) and
x[1] > y[1])
for i in range(len(L)-1):
L[:] = [x for x in L for y in L if betterItem(x, y)]
Is there a better/faster/more Pythonic way of doing this?
Thanks for the help!
results in
(This list comprehension uses a bit of sleight-of-hand:
seen.updatealways returnsNone, andNone or xalways returnsx– soseen.update(i) or (i,j)returns the tuple(i,j)with the side-effect of updating the seen-member list.)This should be O(n log n) due to the
sort, instead of your O(n^2).