我正在查看关于递归的网站 。当我发现使用递归找人名单的排列的这个例子:使用子列表查找列表排列
def fill_seats(people):
# Return a list of ways to fill len(people) seats with the people
# named in the sequence people.
if len(people) == 0:
return []
else:
possible = []
for i in range(len(people)):
this_person = people[i]
everyone_else = people[:i] + people[i+1:]
for seating_arrangement in fill_seats(everyone_else):
possible = possible + [[this_person] + seating_arrangement]
return possible
our_class = ["Biella", "Gwen", "Katie", "Seth", "Will"]
for arrangement in fill_seats(our_class):
print arrangement
print len(fill_seats(our_class)), "total possible arrangements."
但是它使返回0
,我不知道为什么,任何想法?在这种情况下子串是如何工作的?难道他们不仅仅将单个项目拼接在一起?那有什么目的?
不会返回一个列表的列表? –
@AaronHall它将返回一个字符串列表,但是该字符串列表在'[[this_person] + seating_arrangement]'中连接。所以,那应该没问题。 – thefourtheye
如果人是一个字符串,它的len可能会> 1,对不对? –