而不是添加原始的新引用/副本,无论何时看到或添加到最新的一个,都要开始新numbers
的list
,否则:list
1
list_number = [] numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 11, 12, 13, 1, 21, 22, 25, 6, 1, 34, 5, 6, 7, 78] for x in numbers: if x==1: list_number.append([1]) else: list_number[-1].append(x) print list_number
结果:
>>> for x in list_number: ... print x ... [1, 2, 3, 4, 5, 6, 7, 8, 9] [1, 11, 12, 13] [1, 21, 22, 25, 6] [1, 34, 5, 6, 7, 78]