首页 文章

Python groupby通过分隔符拆分列表

提问于
浏览
1

我是Python(3.6)的新手,并且正在努力理解itertools groupby . 我有以下包含整数的列表:

list1 = [1, 2, 0, 2, 3, 0, 4, 5, 0]

但是列表也可能更长,并且在每对数字之后不必出现'0' . 它也可以是3,4或更多的数字 . 我的目标是将此列表拆分为子列表,其中“0”用作分隔符,并且不会出现在任何这些子列表中 .

list2 = [[1, 2], [2, 3], [4, 5]]

这里已经解决了类似的问题:Python spliting a list based on a delimiter word答案2似乎对我有很大帮助,但不幸的是它只给了我一个TypeError .

import itertools as it

    list1 = [1, 2, 0, 2, 3, 0, 4, 5, 0]

    list2 = [list(group) for key, group in it.groupby(list1, lambda x: x == 0) if not key]

    print(list2)

文件“H:/Python/work/ps0001/example.py”,第13行,在list2 = [list(group)for key,group in it.groupby(list,lambda x:x =='0')if不是键] TypeError:'list'对象不可调用

我很感激任何帮助,并很高兴终于了解groupby .

6 回答

  • 2

    您正在检查"0"(str),但您的列表中只有0(int) . 此外,您使用 list 作为第一个列表的变量名称,这是Python中的关键字 .

    from itertools import groupby
    
    list1 = [1, 2, 0, 2, 7, 3, 0, 4, 5, 0]
    list2 = [list(group) for key, group in groupby(list1, lambda x: x == 0) if not key]
    
    print(list2)
    

    这应该给你:

    [[1, 2], [2, 7, 3], [4, 5]]
    
  • 0

    您可以使用正则表达式:

    >>> import ast 
    >>> your_list = [1, 2, 0, 2, 3, 0, 4, 5, 0]
    >>> a_list = str(your_list).replace(', 0,', '], [').replace(', 0]', ']')
    >>> your_result = ast.literal_eval(a_list)
    >>> your_result
    ([1, 2], [2, 3], [4, 5])
    >>> your_result[0]
    [1, 2]
    >>>
    

    或单线解决方案:

    ast.literal_eval(str(your_list).replace(', 0,', '], [').replace(', 0]', ']'))
    
  • 5

    在您的代码中,您需要将 lambda x: x == '0' 更改为 lambda x: x == 0 ,因为您使用的是 int 列表,而不是 str 列表 .

    由于其他人已经展示了如何使用 itertools.groupby 改进您的解决方案,您也可以在没有库的情况下执行此任务:

    >>> list1 = [1, 2, 0, 2, 3, 0, 4, 5, 0]
    >>> zeroes = [-1] + [i for i, e in enumerate(list1) if e == 0]
    >>> result = [list1[zeroes[i] + 1: zeroes[i + 1]] for i in range(len(zeroes) - 1)]
    >>> print(result)
    [[1, 2], [2, 3], [4, 5]]
    
  • 2

    您可以在Loop中执行此操作,如下面注释的Snippet所示:

    list1       = [1, 2, 0, 2, 3, 0, 4, 5, 0]
    tmp,result  = ([],[])   # tmp HOLDS A TEMPORAL LIST :: result => RESULT
    
    for i in list1:
        if not i:
            # CURRENT VALUE IS 0 SO WE BUILD THE SUB-LIST
            result.append(tmp)
            # RE-INITIALIZE THE tmp VARIABLE
            tmp = []
        else:
            # SINCE CURRENT VALUE IS NOT 0, WE POPULATE THE tmp LIST
            tmp.append(i)
    
    print(result) # [[1, 2], [2, 3], [4, 5]]
    

    有效:

    list1       = [1, 2, 0, 2, 3, 0, 4, 5, 0]
    tmp,result  = ([],[])   # HOLDS A TEMPORAL LIST
    
    for i in list1:
        if not i:
            result.append(tmp); tmp = []
        else:
            tmp.append(i)
    
    print(result) # [[1, 2], [2, 3], [4, 5]]
    
  • 0

    使用zip返回列表元组并稍后将它们转换为列表

    >>> a
    [1, 2, 0, 2, 3, 0, 4, 5, 0]
    >>> a[0::3]
    [1, 2, 4]
    >>> a[1::3]
    [2, 3, 5]
    >>> zip(a[0::3],a[1::3])
    [(1, 2), (2, 3), (4, 5)]
    >>> [list(i) for i in zip(a[0::3],a[1::3])]
    [[1, 2], [2, 3], [4, 5]]
    
  • 1

    尝试使用 join 然后拆分 0

    lst = [1, 2, 0, 2, 3, 0, 4, 5, 0]
    
    lst_string = "".join([str(x) for x in lst])
    lst2 = lst_string.split('0')
    lst3 = [list(y) for y in lst2]
    lst4 = [list(map(int, z)) for z in lst3]
    print(lst4)
    

    在我的控制台上运行:

    enter image description here

相关问题