python - Find unique pairs in list of pairs -
i have (large) list of lists of integers, e.g.,
a = [ [1, 2], [3, 6], [2, 1], [3, 5], [3, 6] ]
most of pairs appear twice, order of integers doesn't matter (i.e., [1, 2]
equivalent [2, 1]
). i'd find pairs appear once, , boolean list indicating that. above example,
b = [false, false, false, true, false]
since a
typically large, i'd avoid explicit loops. mapping frozenset
s may advised, i'm not sure if that's overkill.
ctr = counter(frozenset(x) x in a) b = [ctr[frozenset(x)] == 1 x in a]
we can use counter counts of each list (turn list frozenset ignore order) , each list check if appears once.
Comments
Post a Comment