Take a look at itertools.groupby
:
In [1]: from itertools import groupby
In [2]: lst = [[ 'something', ',', 'eh' ], ',', ['more'], ',', 'yet more', '|', 'even more' ]
In [3]: [list(group) for key, group in groupby(lst, lambda x: x!=',') if key]
Out[3]: [[['something', ',', 'eh']], [['more']], ['yet more', '|', 'even more']]
基本的に、リスト内の項目を基準 ( ) に基づいてグループに分割しitem != ','
、理解度チェック により、 に等しい項目であるif k
グループを除外します。False
','
In [4]: for key, group in groupby(lst, lambda x: x!=','):
...: print key, list(group)
...:
True [['something', ',', 'eh']]
False [',']
True [['more']]
False [',']
True ['yet more', '|', 'even more']