Hi I trying to create function with string argument and return ordered permutation dict. Example:
word = 'abc'; func(word) - > [a, b, c, ab, ac, bc, abc]
Hi I trying to create function with string argument and return ordered permutation dict. Example:
word = 'abc'; func(word) - > [a, b, c, ab, ac, bc, abc]
itertools.combinations returns in lexicographical order
Somthing like
def word(w):
for i in range(len(w)):
yield from itertools.combinations(w,i+1)
Edir:
This is wrong just like powerset, because it does not return all results in lexicographical order.
I think you are referring to a combination. Permutations are combinations where the different orders in the elements take importance. If combinations of 'ab' look like ['a', 'b', 'ab'], a permutation looks like ['a', 'b', 'ab', 'ba']
from itertools import combinations
word = 'abc'
my_list = []
for n in range(len(word)):
my_list.extend([''.join(x) for x in combinations(word, n + 1)])
print(my_list)
Result:
['a', 'b', 'c', 'ab', 'ac', 'bc', 'abc']
def toString(List):
return ''.join(List)
def permute(a, l, r):
if l==r:
print toString(a)
else:
for i in xrange(l,r+1):
a[l], a[i] = a[i], a[l]
permute(a, l+1, r)
a[l], a[i] = a[i], a[l]
string = "ABC"
n = len(string)
a = list(string)
permute(a, 0, n-1)