我有一个可以包含任意数量元素的列表。
['jeff','ham','boat','','my','name','hello']
如何根据空白字符串元素将这个列表拆分为两个列表或任意数量的列表?
然后可以将所有这些列表放入一个列表列表中。
这是使用简单迭代的一种方法。
前任:
myList = ['jeff','ham','boat','','my','name','hello']
result = [[]]
for i in myList:
if not i:
result.append([])
else:
result[-1].append(i)
print(result)
输出:
[['jeff', 'ham', 'boat'], ['my', 'name', 'hello']]
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句