Finding a way to list all the combinations of any-3-elements in a list:
Here is what I tried:
import itertools
the_list = ["Alpha","Beta","Gamma","Delta","Epsilon","Zeta"]
list_of_trios = [(the_list[p1], the_list[p2], the_list[p3]) for p1 in range(len(the_list)) for p2 in range(p1 1,len(the_list)) for p3 in range(p1 2,len(the_list))]
tem_list = []
for each in list_of_trios:
check_me = list(set(each))
if len(check_me) == 3:
tem_list.append(check_me)
tem_list.sort()
final_list = list(tem_list for tem_list, _ in itertools.groupby(tem_list))
for ox in final_list:
print (ox)
It seems work. What would be the better way to achieve such? Thank you.
CodePudding user response:
https://docs.python.org/3/library/itertools.html?highlight=comb#itertools.combinations
from itertools import combinations
the_list = ["Alpha","Beta","Gamma","Delta","Epsilon","Zeta"]
list_of_trios = list(combinations(the_list, 3))