Search code examples
pythonstringpermutationpython-itertools

Permute string by indices


Find all distinct permutations of a string where only certain indices are allowed to be permuted.

e.g string_perms('test_world', [2,3]) --> ['test_world', 'tets_world']

I have an answer but it looks very dirty. Is there a more elegant way to do it?

from itertools import permutations

def string_perms(s, indices):
    final = []
    target = [let for ind,let in enumerate(s) if ind in indices]

    perms = list(set(permutations(target)))
    temp = list(s)

    for perm in perms:
        for ind,let in enumerate(perm):
            temp[indices[ind]] = let
        final.append(''.join(temp))
    return final

Solution

  • You can use an iterator with a list comprehension:

    import itertools as it
    def string_perms(s, indices):
       for _i in it.permutations([s[j] for j in indices], len(indices)):
          i = iter(_i)
          yield ''.join(a if j not in indices else next(i) for j, a in enumerate(s))
    
    print(list(string_perms('test_world', [2,3])))
    

    Output:

    ['test_world', 'tets_world']