'producing all the anagrams from a string python
I was thinking about this problem today, and I came with the following pseudocode (Python 3.2) :
def anagrams( string ):
for c in string:
anagram = c + anagram( string - {c} ) # remove the char from its position in the string
print(anagram)
return
def main():
word = "abcd"
anagrams( word )
return
However, I'd like to know a pythonic way to do this operation: anagram = c + anagram( string - {c} )
How could I remove that char from the string? so for example:
"abc" -> 'a' + "bc" -> 'a' + 'b' + "c" -> 'a' + 'b' + 'c' = 'abc'
+ "cb" -> 'a' + 'c' + "b" -> 'a' + 'c' + 'b' = 'acb'
-> 'b' + "ac" -> 'b' + 'a' + "c" -> 'b' + 'a' + 'c' = 'bac'
+ "ca" -> 'b' + 'c' + "a" -> 'b' + 'c' + 'a' = 'bca'
-> 'c' + "ba" -> 'c' + 'b' + "a" -> 'c' + 'b' + 'a' = 'cba'
+ "ab" -> 'c' + 'a' + "b" -> 'c' + 'a' + 'b' = 'cab'
Thanks
Solution 1:[1]
Use the itertools module.
import itertools
perms = [''.join(perm) for perm in itertools.permutations('abc')]
Solution 2:[2]
Just to note, @sloth's answer gives a slightly unexpected result if the string contains more than one instance of a letter - duplicate permutations:
["".join(perm) for perm in itertools.permutations('aabc')]
Results in:
['aabc',
'aacb',
'abac',
'abca',
'acab',
'acba',
'aabc',
'aacb',
'abac',
'abca',
'acab',
'acba',
'baac',
'baca',
'baac',
'baca',
'bcaa',
'bcaa',
'caab',
'caba',
'caab',
'caba',
'cbaa',
'cbaa']
If this isn't the desired result, using 'set' will eliminate the dups. Although if you want a list, you'll need to relist (also 'set' doesn't maintain order so use 'sorted'):
sorted(set(["".join(perm) for perm in itertools.permutations("aabc")]))
Results in:
['aabc',
'aacb',
'abac',
'abca',
'acab',
'acba',
'baac',
'baca',
'bcaa',
'caab',
'caba',
'cbaa']
Solution 3:[3]
You can cast the word to a list, run remove and then use join to get it back together again.
word = 'abca'
letters = list(word)
letters.remove('a') # Only deletes the first 'a'
word = ''.join(letters)
Solution 4:[4]
Here's some of the suggestions wrapped up to make a new script, called anagram
$ anagram hit
hit
hti
iht
ith
thi
tih
anagram
#!/usr/bin/env python3
import argparse
import itertools
import sys
parser = argparse.ArgumentParser()
parser.add_argument("word", help="A word to generate anagrams for.")
args = parser.parse_args()
perms = [''.join(perm) for perm in itertools.permutations(args.word)]
for perm in perms:
print(perm)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | deepstructure |
| Solution 3 | mjgpy3 |
| Solution 4 | Brad Parks |
