简体   繁体   English

按特定值拆分列表

[英]Split list at a specific value

I am trying to write a code that splits lists in a class of lists in two when a certain value is a middle element of the list and then produce two lists where the middle element becomes the end element in the first list and the first element in the second one.我正在尝试编写一个代码,当某个值是列表的中间元素时,将一类列表中的列表分成两部分,然后生成两个列表,其中中间元素成为第一个列表中的结束元素和第二个。

There can be more than n middle elements in the list so the result must be n+1 lists.列表中的中间元素可以超过 n 个,因此结果必须是 n+1 个列表。

Example:例子:

A = [[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15],[16,17,18,19,20,21,22,23,24,25],[26,27,28,29]]

P = [4,7,13,20]
n = len(Points) # in this case n = 4

I am looking for a result that looks like this:我正在寻找如下所示的结果:

A = [[0,1,2,3,4],[4,5,6,7],[7,8,9,10,11,12,13],[13,14,15],[16,17,18,19,20],[20,21,22,23,24,25],[26,27,28,29]]

Since n = 4 and it will produce 5 lists, note that the answer has 6 lists because the last list doesn't have any value of P in and therefore stays intact.由于 n = 4 并且它将产生 5 个列表,请注意答案有 6 个列表,因为最后一个列表没有任何 P in 值,因此保持不变。

I haven't been able to produce anything as I am new to python and it is hard to formulate this problem.由于我是 python 的新手,因此我无法产生任何东西,并且很难制定这个问题。

Any help is appreciated!任何帮助表示赞赏!

You can keep appending the sub-list items to the last sub-list of the output list, and if the current item is equal to the next item in Points , append a new sub-list to the output with the same item and pop the item from Points :您可以继续将子列表项附加到输出列表的最后一个子列表中,如果当前项等于Points的下一项,则将具有相同项的新子列表附加到输出并弹出来自Points项目:

output = []
for l in List:
    output.append([])
    for i in l:
        output[-1].append(i)
        if Points and i == Points[0]:
            output.append([i])
            Points.pop(0)

With your sample input, output would become:使用您的样本输入, output将变为:

[[0, 1, 2, 3, 4], [4, 5, 6, 7], [7, 8, 9, 10, 11, 12, 13], [13, 14, 15], [16, 17, 18, 19, 20], [20, 21, 22, 23, 24, 25], [26, 27, 28, 29]]

You can first recover all indices of the provided values and then slice accordingly.您可以首先恢复所提供值的所有索引,然后相应地切片。

Code代码

def split_at_values(lst, values):
    indices = [i for i, x in enumerate(lst) if x in values]
    for start, end in zip([0, *indices], [*indices, len(lst)]):
        yield lst[start:end+1]

Example例子

values =  {4, 7, 13, 20}
lst = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]

print(*split_at_values(lst, values))

Output输出

[0, 1, 2, 3, 4] [4, 5, 6, 7] [7, 8, 9, 10, 11, 12, 13] [13, 14, 15]

You can then apply this iteratively to you input list A to get the desired result.然后,您可以将此迭代应用于输入列表A以获得所需的结果。 Alternatively you can use itertools.chain.from_iterable .或者,您可以使用itertools.chain.from_iterable

from itertools import chain

values = {4, 7, 13, 20}
lst_A = [[0, 1, 2, 3, 4, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
         [16, 17, 18, 19, 20, 21, 22, 23, 24, 25],
         [26, 27, 28, 29]]

output = list(chain.from_iterable(split_at_values(sublst, values) for sublst in lst_A))

print(output)

Output输出

[[0, 1, 2, 3, 4],
 [4, 5, 6, 7],
 [7, 8, 9, 10, 11, 12, 13],
 [13, 14, 15],
 [16, 17, 18, 19, 20],
 [20, 21, 22, 23, 24, 25],
 [26, 27, 28, 29]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM