Skip to content

Instantly share code, notes, and snippets.

@czheo
Last active December 12, 2018 11:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save czheo/37710f057af62c4d8933bbb8cad98b7d to your computer and use it in GitHub Desktop.
Save czheo/37710f057af62c4d8933bbb8cad98b7d to your computer and use it in GitHub Desktop.
l = [("a", "b"), ("b", "c"), ("e", "f")]
def merge(l):
ret = []
for s in map(set, l):
for x in ret:
if s & x:
x.update(s)
break
else:
ret.append(s)
return ret
print(merge(l))
# [{'b', 'c', 'a'}, {'f', 'e'}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment