该程序是:请输入3位数字:635排列为:635、653、365、356、563、536
def permutation_generate(lst):
if len(lst)== 0:
return []
if len(lst)==1:
return [1]
l =[]
for i in range(len(lst)):
m = lst[i]
remLst= lst[:i]+ lst[i+1:]
for p in permutation_generate(remLst):
l.append([m]+p)
return l
data= list(input("Please enter a 3 digits number:\n"))
print("The permutations are:")
for p in permutation_generate(data):
print(*p,sep='', end="")
#TypeError:只能列表列表(而不是“ int”) #错误的解决方案是什么?
def permutation_generate(lst):
if len(lst)== 0:
return []
if len(lst)==1:
return [1]
l =[]
for i in range(len(lst)):
m = lst[i]
remLst= lst[:i]+ lst[i+1:]
for p in permutation_generate(remLst):
l.append([m]+p)
return l
data= list(input("Please enter a 3 digits number:\n"))
print("The permutations are:")
for p in permutation_generate(data):
print(*p,sep='', end="")
#TypeError: can only concatenate list (not "int") to list
#What is the solution of the error?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在第13行,您的函数
repernaint_generate(remlst)
返回列表[1]
然后,您开始在循环的
中迭代它。因此p是
1
typeint
这就是为什么在第14行上您会遇到错误的错误[m]+int无法正常工作的原因。 [M]+[P]将起作用。
为了您的任务,请查看Python标准库的Itertools模块
On line 13 your function
permutation_generate(remLst)
returns a list[1]
and you start iterating over it in a
for
loop. So p is1
of typeint
thats why on line 14 you get the error trying to concatenate [m]+int won't work. [m]+[p] would work.
And for your task take a look at itertools module of python standard library