Reputation: 75
I am trying to create a list of subsets of n
of length 3. However, my approaches are quite slow, and it takes a while to run the whole program.
My "code" so far is just the generic iterative approach.
subs = []
for a in vertices:
for b in vertices:
for c in vertices:
if sorted([a, b, c]) not in subs and a != b and b != c:
subs.append(sorted([a, b, c]))
This is quite slow, but I'm not sure exactly how I could speed it up. Could anyone help?
Upvotes: 2
Views: 300
Reputation: 453
Try
import itertools
xs = [1, 2, 3,4]
print(list(itertools.combinations(xs, 3)))
[(1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4)]
Upvotes: 2