pythonscramble

Is it possible to reorginize characters in every possible way in python and how?


I am creating a basic program to take words turn them into single characters and then scrabble all of the characters in every way possible. The only issue I am having is scrabbling the characters in everyway.

In every way possible I mean like say you have the word "the" you could have "hte" and "eth" and so on and so forth.

My code so far:

user = input("First; ")
user2 = input("Second; ")
user3 = input("Third; ")
user4 = input("Fourth; ")
user5 = input("Fifth; ")

with open("file.txt", 'r+') as file:
    file.truncate()
    file.write(user + user2 + user3 + user4 + user5)

xy = open("file.txt", "r")
yy = xy.read()
wow = list (yy)

Solution

  • You can use itertools.permutations to generate anagrams:

    import itertools
    anagrams = [''.join(x) for x in itertools.permutations(word)]
    

    If a word contains repeated letters, you will get some duplicates. You can remedy this using set():

    anagrams = list(set(anagrams))