Python way of doing difference of lists containing lists -
consider 2 lists , b. know list(set(a) - set(b))
give difference between , b. situation whereby elements in both , b lists. i.e. , b list of list? e.g.
a = [[1,2], [3,4], [5,6]] b = [[3,4], [7,8]]
i wish return difference a - b
list of list i.e. [[1,2],[5,6]]
list(set(a) - set(b)) typeerror: unhashable type: 'list'
the idea convert list of lists lists of tuples,
hashable , are, thus, candidates of making sets themselves:
in [192]: c = set(map(tuple, a)) - set(map(tuple, b)) in [193]: c out[193]: {(1, 2), (5, 6)}
and 1 more touch:
in [196]: [*map(list, c)] out[196]: [[1, 2], [5, 6]]
added
in python 2.7 final touch simpler:
map(list, c)
Comments
Post a Comment