简体   繁体   English

如何按条件将列表拆分为列表?

[英]How to split list to lists by condition?

I want split a list into multiple lists, based on a condition. 我想根据条件将一个列表分成多个列表。 If the difference in the series grows more than 4, then it is trigger to split the list till that item from last split item. 如果系列中的差异大于4,则触发将列表拆分到最后拆分项目中的该项目。

For example: 例如:

in = [1,2,3,9,10,11,100,200]
out = [ [1,2,3 ], [ 9,10,11 ], [100], [200]  ]

by condition 根据条件

If (next - prev) > 4
def splitlist(L):
    if not L: return []
    answer = [[L[0]]]

    for i in L[1:]:
        if i - answer[-1][-1] < 4:
            answer[-1].append(i)
        else:
            answer.append([i])
    return answer

Output: 输出:

In [112]: splitlist([1,2,3,9,10,11,100,200])
Out[112]: [[1, 2, 3], [9, 10, 11], [100], [200]]

NOTICE: in and next are Python keywords and builtin functions respectively. 注意: innext是Python关键字和内置函数。

The trick is to use zip() function with 2 slices of the same list, but shifted on one item: 诀窍是将zip()函数与同一列表的2个切片一起使用,但只转移了一项:

in_list = [1, 2, 3, 9, 10, 11, 100, 200]

parts = [[]]
for prev_item, next_item in zip(in_list[:-1], in_list[1:]):
    top = parts[-1]
    top.append(prev_item)
    if next_item - prev_item > 4:
        parts.append([])
top = parts[-1]
top.append(in_list[-1])

The output is: 输出为:

[[1, 2, 3], [9, 10, 11], [100], [200]]

Short solution using numpy module: 使用numpy模块的简短解决方案:

import numpy as np

arr = np.array([1,2,3,9,10,11,100,200])
out = [a.tolist() for a in np.split(arr, np.where(np.diff(arr) > 4)[0]+1)]
print(out)

The output: 输出:

[[1, 2, 3], [9, 10, 11], [100], [200]]

  • np.where(np.diff(arr) > 4) - find the array indices where condition "difference between next value and previous value is greater than 4" is met np.where(np.diff(arr) > 4) -查找满足条件“下一个值与上一个值之间的差大于4”的数组索引

  • np.split(x, indices) - split the initial array by crucial indices np.split(x, indices) -通过关键索引分割初始数组

One of the possible approaches: 可能的方法之一:

ls = [1,2,3,9,10,11,100,200, 201, 209]

#output list
output = list()
startind = 0

for ind, val in enumerate(ls):
    if ls[ind]-ls[ind-1] > 4:
        output.append(ls[startind:ind])
        startind = ind

# appending the left over from last detected difference of 4
output.append(ls[startind:])
print output

Output: [[1, 2, 3], [9, 10, 11], [100], [200, 201], [209]] 输出:[[1、2、3],[9、10、11],[100],[200、201],[209]

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

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