[英]Break list into list of lists based on condition
I have the following lists x1, x2, x3, which i want to chunk into the respective outputs mentioned below:我有以下列表 x1、x2、x3,我想将它们分块到下面提到的相应输出中:
x1 = ['req', 'a', 'b', 'c', 'req', 'd', 'e', 'req', 'f']
expected_out1 = [['req', 'a', 'b', 'c'], ['req', 'd', 'e'], ['req', 'f']]
x2 = ['req', 'a', 'b', 'c', 'req', 'd', 'e', 'req', 'f', 'req']
expected_out2 = [['req', 'a', 'b', 'c'], ['req', 'd', 'e'], ['req', 'f'], ['req']]
x3 = ['req', 'a', 'b', 'c', 'req', 'd', 'e', 'req', 'req']
expected_out3 = [['req', 'a', 'b', 'c'], ['req', 'd', 'e'], ['req'], ['req']]
I wrote the following code to address these scenarios:我编写了以下代码来解决这些情况:
import numpy as np
def split_basedon_condition(b):
num_arr = np.array(b)
arrays = np.split(num_arr, np.where(num_arr[:-1] == "req")[0])
return [i for i in [i.tolist() for i in arrays] if i != []]
But I'm getting the following results:但我得到以下结果:
split_basedon_condition(x1)
actual_out1 = [['req', 'a', 'b', 'c'], ['req', 'd', 'e'], ['req', 'f']] # expected
split_basedon_condition(x2)
actual_out2 = [['req', 'a', 'b', 'c'], ['req', 'd', 'e'], ['req', 'f', 'req']] # not expected
split_basedon_condition(x3)
actual_out3 = [['req', 'a', 'b', 'c'], ['req', 'd', 'e'], ['req', 'req']] # not expected
Reason is in this line:原因在这一行:
arrays = np.split(num_arr, np.where(num_arr[:-1] == "req")[0])
By doing num_arr[:-1]
you are considering num_arr with jettisoned last element, thus this behavior when "req"
is last element.通过执行num_arr[:-1]
您正在考虑 num_arr 放弃最后一个元素,因此当"req"
是最后一个元素时这种行为。 Replace above line with:将上面的行替换为:
arrays = np.split(num_arr, np.where(num_arr == "req")[0])
and it will work as excepted for all test cases you provided.并且对于您提供的所有测试用例,它都可以正常工作。
As side note if you are allowed to use external python libraries other than numpy
, you might harness more_itertools.split_before for that task.作为旁注,如果您被允许使用除numpy
之外的外部 python 库,您可以利用more_itertools.split_before来完成该任务。
Here's much faster solution pure python.这是更快的纯 python 解决方案。
def split(arr, pred):
j = len(arr)
for i,_ in filter(lambda x: x[1] == pred, zip(range(j-1, -1, -1), reversed(arr))):
yield arr[i:j]
j = i
list(split(['req', 'a', 'b', 'c', 'req', 'd', 'e', 'req', 'req'], 'req'))
# [['req'], ['req'], ['req', 'd', 'e'], ['req', 'a', 'b', 'c']]
If the first one always is "req"
.This is one-line:如果第一个总是"req"
。这是一行:
def func(l):
return list(map(lambda x: x.insert(0, "req") or x, map(list, "".join(l[1:]).split("req"))))
Result:结果:
[['req', 'a', 'b', 'c'], ['req', 'd', 'e'], ['req', 'f']]
[['req', 'a', 'b', 'c'], ['req', 'd', 'e'], ['req', 'f'], ['req']]
[['req', 'a', 'b', 'c'], ['req', 'd', 'e'], ['req'], ['req']]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.