find the "overlap" between 2 python lists -
given 2 lists:
a = [3,4,5,5,5,6] b = [1,3,4,4,5,5,6,7]
i want find "overlap":
c = [3,4,5,5,6]
i'd if extract "remainder" part of , b that's not in c.
a_remainder = [5,] b_remainder = [1,4,7,]
note: has 3 5's in , b has two. b has 2 4's in , has one.
the resultant list c should have 2 5's (limited list b) , 1 4 (limited list a).
this gives me want, can't think there's better way.
import copy = [3,4,5,5,5,6] b = [1,3,4,4,5,5,6,7] c = [] elem in copy.deepcopy(a): if elem in b: a.pop(a.index(elem)) c.append(b.pop(b.index(elem))) # , b both contain "remainders" , c contains "overlap"
on note, more accurate name i'm asking "overlap" , "remainder"?
collection.counter
available in python 2.7 can used implement multisets want.
a = [3,4,5,5,5,6] b = [1,3,4,4,5,5,6,7] a_multiset = collections.counter(a) b_multiset = collections.counter(b) overlap = list((a_multiset & b_multiset).elements()) a_remainder = list((a_multiset - b_multiset).elements()) b_remainder = list((b_multiset - a_multiset).elements()) print overlap, a_remainder, b_remainder
Comments
Post a Comment