我正在查看關於遞歸的網站 。當我發現使用遞歸找人名單的排列的這個例子:使用子列表查找列表排列
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,對不對? –