def myLists(L):
final_list=[]
sub_list=[]
for each in L:
sub_list[:]=[]
for num in range(each+1):
sub_list.append(num)
#remove 0 from the list
sub_list=sub_list[1:]
final_list.append(sub_list)
return final_list
L=[1,2,4,5,7]
nl= myLists(L)
print nl
预期的输出是:
[[1], [1, 2], [1, 2, 3], [1, 2, 3, 4], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5, 6, 7]]
我得到如下输出,不明白为什么在子列表中添加了“0”:
[[0, 1, 2], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4, 5, 6, 7], [1, 2, 3, 4, 5, 6, 7]]