我今天正在考虑这个问题,我带来了以下伪代码(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
但是,我想知道执行此操作的 pythonic 方式: anagram = c + anagram( string - {c} )
如何从字符串中删除该字符?例如:
"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'
谢谢