如何生成字符串的所有排列? [英] How to generate all permutations of a string?

查看:64
本文介绍了如何生成字符串的所有排列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我想生成一个字符串的所有排列。我设法

生成所有循环排列。请帮忙:)


def permute(string):

l = []

l.append(string)

string1 =''''

for i in range(0,len(string)-1,1):

string1 = string [1: len(string)] + string [:1]

l.append(string1)

string = string1

return l

解决方案

在文章< ma ************************ ***************@蟒蛇。 org>,

" Girish Sahani" < GI **** @ cse.iitb.ac.in>写道:

我想生成一个字符串的所有排列。




def permute(Seq):

"""生成元素的连续排列

of Seq。""

if len(Seq)== 0:

收益率()

否则:

for i in range(0,len(Seq)):

for rest in permute(Seq [:i] + Seq [i + 1:]):

yield(Seq [i],)+ rest

#end for

#end for

#end if

#end permute


Mind ,劳伦斯的解决方案可能包含双打:

[i for i in permute(" aa" )]



[(''a'',''a''),('''',''a'') ]


如果你不想要这个,请使用套装。


>在文章< ma *************************************** @ python。 org>,

" Girish Sahani" < GI **** @ cse.iitb.ac.in>写道:

我想生成一个字符串的所有排列。
def permute(Seq):
""生成连续排列的生成器Seq。""
如果len(Seq)== 0:
yield()
其他:
我在范围内( 0,len(Seq)):
在permute中休息(Seq [:i] + Seq [i + 1:]):
yield(Seq [i],)+ rest
#end for
#end for
#end if
#end permute



感谢lawrence ...但是这个函数不会返回置换字符串,所以

i添加了一些如下代码来生成所有排列的列表:


def permute(seq):

l = []

如果len(seq)== 0:

收益率[]

否则:

for i在范围内(0,len(seq)):

在permute中休息(seq [:i] + seq [i + 1:]):

yield(seq) [i],)+休息

for t在permute(seq):

l.append(''''。join(t))

返回l


这给了我一个语法错误!

我需要输出一个包含输入字符串所有排列的列表。

-
http://mail.python.org/mailman/listinfo/python-list




Hi guys,
I want to generate all permutations of a string. I''ve managed to
generate all cyclic permutations. Please help :)

def permute(string):
l= []
l.append(string)
string1 = ''''
for i in range(0,len(string)-1,1):
string1 = string[1:len(string)] + string[:1]
l.append(string1)
string = string1
return l

解决方案

In article <ma***************************************@python. org>,
"Girish Sahani" <gi****@cse.iitb.ac.in> wrote:

I want to generate all permutations of a string.



def permute(Seq) :
"""generator which yields successive permutations of the elements
of Seq."""
if len(Seq) == 0 :
yield ()
else :
for i in range(0, len(Seq)) :
for rest in permute(Seq[:i] + Seq[i + 1:]) :
yield (Seq[i],) + rest
#end for
#end for
#end if
#end permute


Mind, that Lawrence''s solution may contain doubles:

[ i for i in permute("aa") ]


[(''a'', ''a''), (''a'', ''a'')]

If you don''t want this, use sets.


> In article <ma***************************************@python. org>,

"Girish Sahani" <gi****@cse.iitb.ac.in> wrote:

I want to generate all permutations of a string.
def permute(Seq) :
"""generator which yields successive permutations of the elements
of Seq."""
if len(Seq) == 0 :
yield ()
else :
for i in range(0, len(Seq)) :
for rest in permute(Seq[:i] + Seq[i + 1:]) :
yield (Seq[i],) + rest
#end for
#end for
#end if
#end permute


thanks lawrence...however this func doesnt return the permuted strings, so
i added some code as follows to generate a list of all the permutations:

def permute(seq):
l = []
if len(seq) == 0:
yield []
else:
for i in range(0,len(seq)):
for rest in permute(seq[:i] + seq[i+1:]):
yield (seq[i],) + rest
for t in permute(seq):
l.append(''''.join(t))
return l

This gives me a syntax error!
I need to output a list that has all the permutations of the input string.
--
http://mail.python.org/mailman/listinfo/python-list




这篇关于如何生成字符串的所有排列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆