how can combinations of several letters in python 3 without using itertools? example, have a,b,c, how possible combinations including a,b,c,ab,ba,ac,ca,bc,cb,abc,acb,bac,bca,cab,cba
?
itertools.combinations
equal following functions pyhtoinic way go calculating combinations :
def combinations(iterable, r): # combinations('abcd', 2) --> ab ac ad bc bd cd # combinations(range(4), 3) --> 012 013 023 123 pool = tuple(iterable) n = len(pool) if r > n: return indices = list(range(r)) yield tuple(pool[i] in indices) while true: in reversed(range(r)): if indices[i] != + n - r: break else: return indices[i] += 1 j in range(i+1, r): indices[j] = indices[j-1] + 1 yield tuple(pool[i] in indices)
if don't want import itertools
module can use function.
Comments
Post a Comment