Skip to content

Instantly share code, notes, and snippets.

@abijith-kp
Created July 25, 2014 07:52
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save abijith-kp/ae260a89da5a9b67457b to your computer and use it in GitHub Desktop.
Merges a list of lists, in such a way that if any of the inner lists have same elements.
from timeit import Timer
def uniq(a):
l = []
[l.append(i) for i in a if i not in l]
return l
def intersect(a, b):
return [i for i in a if i in b]
def union(a, b):
return uniq(a.__add__(b))
def find_group(a, b):
l = union(a, b)
li = l.index
for i in l:
if i == []:
continue
lx = li(i)
for j in l[lx+1:]:
if j == []:
continue
ly = li(j)
if intersect(i, j) != []:
l[lx] = union(i, j)
i = l[lx]
l[ly] = []
return [i for i in l if i != []]
def test():
a = [[1, 2, 3], [0], [3, 5], [6, 7, 8, 9], [0, 10], [1]]
b = [[3, 11], [0, 13, 10], [18, 19], [10, 10], [1]]
find_group(a, b)
if __name__ == "__main__":
print Timer(test).repeat(repeat=5, number=100)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment