【发布时间】:2012-08-19 02:34:29
【问题描述】:
如果我有一个字符串,我可以使用 str.split 方法将其拆分为空格:
"hello world!".split()
返回
['hello', 'world!']
如果我有一个类似的列表
['hey', 1, None, 2.0, 'string', 'another string', None, 3.0]
有没有split方法可以围绕None进行拆分并给我
[['hey', 1], [2.0, 'string', 'another string'], [3.0]]
如果没有内置方法,那么最 Pythonic/优雅的方法是什么?
【问题讨论】:
-
您没有指定
[None, 1, None, None, 2, None]的行为,我认为应该产生[[],[1],[],[2],[]]