Python groupby 按分隔符拆分列表
Python groupby to split list by delimiter
我是 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)
File "H:/Python/work/ps0001/example.py", line 13, in
list2 = [list(group) for key, group in it.groupby(list, lambda x: x == '0') if not key]
TypeError: 'list' object is not callable
我将不胜感激任何帮助,并很高兴终于理解了 groupby。
尝试使用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)
运行 在我的控制台上:
您正在检查“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]]
您可以为此使用正则表达式:
>>> 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]', ']'))
使用 zip 压缩 return 列表元组,稍后在
将它们转换为列表
>>> 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]]
在您的代码中,您需要将 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]]
您可以在一个循环中执行此操作,如下面评论的代码段所示:
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]]
我是 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)
File "H:/Python/work/ps0001/example.py", line 13, in list2 = [list(group) for key, group in it.groupby(list, lambda x: x == '0') if not key]
TypeError: 'list' object is not callable
我将不胜感激任何帮助,并很高兴终于理解了 groupby。
尝试使用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)
运行 在我的控制台上:
您正在检查“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]]
您可以为此使用正则表达式:
>>> 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]', ']'))
使用 zip 压缩 return 列表元组,稍后在
将它们转换为列表>>> 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]]
在您的代码中,您需要将 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]]
您可以在一个循环中执行此操作,如下面评论的代码段所示:
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]]