求一个列表的子列表,两个for和range的搭配我看着有点晕

def allSublists(data):
# Start out with the empty list as the only sublist of data
  sublists = [[]]
# Generate all of the sublists of data from length 1 to len(data)
  for length in range(1, len(data) + 1):
# Generate the sublists starting at each index
    for i in range(0, len(data) - length + 1):
# Add the current sublist to the list of sublists
    sublists.append(data[i : i + length])
# Return the result
  return sublists

RT,两个for和range的搭配看得有点晕,能否详细讲解。
当然我知道这是在动态地写子列表的边界或者范围,但是为啥这么搭配我确实没懂。

用列表推导式一行代码就行:


>>> subLists = lambda L:[L[i:j] for i in range(len(L)) for j in range(i+1,len(L)+1)]
>>> subLists([1,2,3,4])
[[1], [1, 2], [1, 2, 3], [1, 2, 3, 4], [2], [2, 3], [2, 3, 4], [3], [3, 4], [4]]
>>> 

双重循环,遍历出原列表的所有切片, 
L[i:j] 两个循环变量
 i -> 0 ~ len()
 j -> j+1 ~ len()+1
不含右边界