按模式拆分列表列表

时间:2017-01-30 00:45:26

标签: python list split slice

我想通过检查每个子列表的第一个元素是否为“' 0”来分割列表列表,即

original_list = [[0,ab],[1,cd],[1,ef],[0,gh],[1,ij]]

我希望我的结果列表是:

result = [[ab,cd,ef],[gh,ij]]

我想我需要使用字典,但在Python中最有效的方法是什么?

非常感谢!

1 个答案:

答案 0 :(得分:0)

根据您的说法,这可以解决您的问题:

original_list = [[0,'ab'],[1,'cd'],[1,'ef'],[0,'gh'],[1,'ij']]

result = []
temp = []
checker = 0
for item in original_list:
    if item[0] >= checker:
        checker = item[0]
    else:
        result.append(temp)
        checker = 0
        temp = []
    temp.append(item[1])

result.append(temp)
相关问题