【发布时间】:2012-11-04 20:30:28
【问题描述】:
我有一个清单:
lists = (['1','2','3','S','3','4','S','4','6','7'])
我想在每次出现“S”时将列表拆分为更小的列表,并将“S”消除为:
([['1','2','3'],['3','4],['4','6','7']])
我的代码:
def x (lists):
empty = ''
list = []
for x in lists:
if x == empty:
list[-1:].append(x)
else:
list.append([x])
return (list)
我尝试过这样的事情,但我对 python 很陌生,而且我一无所获。没什么特别的,我将如何解决我的问题?
【问题讨论】:
标签: python list for-loop append