How do you generate a permutation of a string in python?

why do you not simple do:

from itertools import permutations
perms = [''.join[p] for p in permutations[['s','t','a','c','k']]]
print perms
print len[perms]
print len[set[perms]]

you get no duplicate as you can see :

 ['stack', 'stakc', 'stcak', 'stcka', 'stkac', 'stkca', 'satck', 'satkc', 
'sactk', 'sackt', 'saktc', 'sakct', 'sctak', 'sctka', 'scatk', 'scakt', 'sckta',
 'sckat', 'sktac', 'sktca', 'skatc', 'skact', 'skcta', 'skcat', 'tsack', 
'tsakc', 'tscak', 'tscka', 'tskac', 'tskca', 'tasck', 'taskc', 'tacsk', 'tacks', 
'taksc', 'takcs', 'tcsak', 'tcska', 'tcask', 'tcaks', 'tcksa', 'tckas', 'tksac', 
'tksca', 'tkasc', 'tkacs', 'tkcsa', 'tkcas', 'astck', 'astkc', 'asctk', 'asckt', 
'asktc', 'askct', 'atsck', 'atskc', 'atcsk', 'atcks', 'atksc', 'atkcs', 'acstk', 
'acskt', 'actsk', 'actks', 'ackst', 'ackts', 'akstc', 'aksct', 'aktsc', 'aktcs', 
'akcst', 'akcts', 'cstak', 'cstka', 'csatk', 'csakt', 'cskta', 'cskat', 'ctsak', 
'ctska', 'ctask', 'ctaks', 'ctksa', 'ctkas', 'castk', 'caskt', 'catsk', 'catks', 
'cakst', 'cakts', 'cksta', 'cksat', 'cktsa', 'cktas', 'ckast', 'ckats', 'kstac', 
'kstca', 'ksatc', 'ksact', 'kscta', 'kscat', 'ktsac', 'ktsca', 'ktasc', 'ktacs', 
'ktcsa', 'ktcas', 'kastc', 'kasct', 'katsc', 'katcs', 'kacst', 'kacts', 'kcsta', 
'kcsat', 'kctsa', 'kctas', 'kcast', 'kcats']
    120
    120
    [Finished in 0.3s]

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    A permutation, also called an “arrangement number” or “order”, is a rearrangement of the elements of an ordered list S into a one-to-one correspondence with S itself. A string of length n has n! permutation. Examples:

    Input :  str = 'ABC'
    Output : ABC 
             ACB 
             BAC 
             BCA 
             CAB 
             CBA

    We have existing solution for this problem please refer Permutations of a given string using STL link. We can also solve this problem in python using inbuilt function permutations[iterable]. 

    Python3

    from itertools import permutations

    def allPermutations[str]:

         permList = permutations[str]

         for perm in list[permList]:

             print [''.join[perm]]

    if __name__ == "__main__":

        str = 'ABC'

        allPermutations[str]

    Output:

    ABC
    ACB
    BAC
    BCA
    CAB
    CBA

    Permutation and Combination in Python Permutations of a given string with repeating characters The idea is to use dictionary to avoid printing duplicates. 

    Python3

    from itertools import permutations

    import string

    s = "GEEK"

    a = string.ascii_letters

    p = permutations[s]

    d = []

    for i in list[p]:

        if [i not in d]:

            d.append[i]

            print[''.join[i]]

    Output:

    GEEK
    GEKE
    GKEE
    EGEK
    EGKE
    EEGK
    EEKG
    EKGE
    EKEG
    KGEE
    KEGE
    KEEG

    Time Complexity: O[n!] where n is the size of the string.
    Auxiliary Space: O[n!] 


    View Discussion

    Improve Article

    Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Given a string, write a Python program to find out all possible permutations of a string. Let’s discuss a few methods to solve the problem.
    Method #1: Using Naive Method 
     

    Python3

    ini_str = "abc"

    print["Initial string", ini_str]

    result = []

    def permute[data, i, length]:

        if i == length:

            result.append[''.join[data] ]

        else:

            for j in range[i, length]:

                data[i], data[j] = data[j], data[i]

                permute[data, i + 1, length]

                data[i], data[j] = data[j], data[i] 

    permute[list[ini_str], 0, len[ini_str]]

    print["Resultant permutations", str[result]]

    Output:

    Initial string abc
    Resultant permutations ['abc', 'acb', 'bac', 'bca', 'cba', 'cab']

      
    Method #2: Using itertools 
     

    Python3

    from itertools import permutations

    ini_str = "abc"

    print["Initial string", ini_str]

    permutation = [''.join[p] for p in permutations[ini_str]]

    print["Resultant List", str[permutation]]

    Output:

    Initial string abc
    Resultant List ['abc', 'acb', 'bac', 'bca', 'cab', 'cba']


    How do you make all combinations of a string?

    Algorithm is copied below. void combine[String instr, StringBuffer outstr, int index] { for [int i = index; i < instr. length[]; i++] { outstr..
    append a character..
    print the result..
    perform a recursive invocation at the level i+1..
    remove the character we added at step 1..

    How do you find the permutation value in Python?

    To calculate permutations in Python, use the itertools. permutation[] method. The itertools. permutations[] method takes a list, dictionary, tuple, or other iterators as a parameter and returns the permutations of that list.

    What is permutation method in Python?

    A permutation, also called an “arrangement number” or “order”, is a rearrangement of the elements of an ordered list S into a one-to-one correspondence with S itself. A string of length n has n! permutation. Examples: Input : str = 'ABC' Output : ABC ACB BAC BCA CAB CBA.

    How do you print 3 permutations in Python?

    python create a program that runs through all possible combinations.
    from itertools import combinations..
    lst = ["a" ,"b", "c"].
    lengthOfStrings = 3..
    for i in combinations[lst, lengthOfStrings]:.
    print[i].

    Chủ Đề