- 帖子
- 4
- 精华
- 0
- 积分
- 245
- 阅读权限
- 30
- 注册时间
- 2016-11-13
- 最后登录
- 2017-9-17
|
各位大佬帮个忙,困扰好几天了,我试着编一个关于全排列的小代码,可是老是显示TypeError: object of type 'NoneType' has no len(),很尴尬。具体报错信息为:Traceback (most recent call last):
File "C:\Users\asus\Desktop\Exercise\00000.py", line 19, in <module>
arrange(member)
File "C:\Users\asus\Desktop\Exercise\00000.py", line 10, in arrange
temp_arrange = arrange(temp_list)
File "C:\Users\asus\Desktop\Exercise\00000.py", line 2, in arrange
if len(member) == 1:
TypeError: object of type 'NoneType' has no len()
我的思路是这样的:想用递归(或者迭代?)的方式:首先对于输入进的n个元素,转化为字符类型存放于列表中,在取出第一个字符类型作为插入元素,依次插入到前面n-1个已经做好全排列的结果中,最后以列表形式输出。预计的结果大概像这样:[['a','s','d'],['a','d','s'],['d','s','a'],['d','a','s']]
下面贴我的代码:
def arrange(member):
if len(member) == 1:
result = []
result.append(member)
print(result)
else:
x = member[0]
temp_list = member.remove(x)
temp_arrange = arrange(temp_list)
for i in temp_arange:
for j in range(len(i)):
i.insert(j,x)
print(i)
member = input("请输入待排列元素:")
member = list(member)
arrange(member)
|
|