Created
June 15, 2022 17:21
-
-
Save MaxHalford/59296e2f6db87c3d82440771631cb0d8 to your computer and use it in GitHub Desktop.
Group by merge
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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