I took a swing at answering a question on StackOverflow, albeit I liked the accepted answer better than mine. But I think my suggested answer should be more simple to digest and provide more control with “allEqual()” function.
This file contains 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 | |
a = [7, 3] | |
b = [3, 1, 2] | |
c = [4, 3, 5] | |
def allEqual(t): | |
same = True | |
if len(t) == 1: | |
return True | |
if len(t) == 0: | |
return False | |
for i in range(1, len(t)): | |
if t[i] != t[i – 1]: | |
same = False | |
i = len(t) – 1 | |
else: | |
same = same and True | |
return same | |
combo = list(itertools.product(a, b, c)) | |
# print list(itertools.permutations(a,2)) | |
# print combo | |
# combo = [x for x in combo if x[0]==x[1]==x[2]] | |
# print combo | |
combo = [x for x in combo if allEqual(x)] | |
print combo |