Skip to content

Instantly share code, notes, and snippets.

@MaxHalford
Created June 15, 2022 17:21
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 MaxHalford/59296e2f6db87c3d82440771631cb0d8 to your computer and use it in GitHub Desktop.
Save MaxHalford/59296e2f6db87c3d82440771631cb0d8 to your computer and use it in GitHub Desktop.
Group by merge
import itertools
def merge_components(cs):
while True:
for (i, a), (j, b) in itertools.combinations(enumerate(cs), 2):
if a[0] == b[0]:
a[1].extend(b[1])
del cs[j]
break
else:
break
return cs
merge_components([
("foo", [1, 2, 3]),
("bar", [1, 2, 3]),
("foo", [1, 2, 3]),
("foo", [1, 2, 3]),
("bar", [1, 2, 3]),
])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment