简体   繁体   English

如何在Python中组织在负值和正值之间交替的数据

[英]How to organize data that alternates between negative and positive values in Python

I have a list of data points in a file that I am inserting into a linked list class that I built. 我有一个文件中的数据点列表,我将其插入到我构建的链接列表类中。
The file is organized such that there are a series of negative values followed by a series of positive values alternating back and forth. 对文件进行组织,使得存在一系列负值,然后是来回交替的一系列正值。 An example: 一个例子:

-2323 

-2324

-53434

-1027

-34232

 343434

 5657

 6565

 6500

-343434

-3434

-565

5845

4667

5453

98356

This pattern continues for many lines. 这种模式持续多行。 The number of negative or positive values is never the same for each section. 每个部分的负值或正值的数量永远不会相同。

I would like to separate these values somehow so that the first list object contains the first set of positive to negative numbers, in this case from -2323 to 6500 . 我想以某种方式分离这些值,以便第一个列表对象包含第一组正数到负数,在这种情况下从-23236500 The next list object would contain the values from -343434 to 98356 , and so on. 下一个列表对象将包含-34343498356的值,依此类推。

I cannot figure out how to get python to know how to separate these sets of data when reading the file. 我无法弄清楚如何让python知道在读取文件时如何分离这些数据集。 Any help would be appreciated! 任何帮助,将不胜感激!

import itertools
groups = itertools.groupby(l, lambda x : x > 0)
result = [list(groups[i][1]) + list(groups[i + 1][1]) for i in range(0, len(groups), 2)]

This will first group the elements by whether they are positive or not, and will then combine adjacent pairs from the groups into a single list, which will then be an element of the result list. 这将首先按元素是否为正对元素进行分组,然后将组中的相邻对组合成单个列表,然后该列表将成为结果列表的元素。

Edit: I keep forgetting that itertools makes objects that don't work like normal iterables. 编辑:我一直忘记了itertools使得对象不像普通的iterables那样工作。

The following should actually work, if a bit more messily. 以下应该实际工作,如果有点混乱。

import itertools
groups = itertools.groupby(l, lambda x : x > 0)
grouplist = [[i for i in y] for (x, y) in groups]
result = [grouplist[i] + grouplist[i + 1] for i in range(0, len(grouplist), 2)]
def takeSection(sequence):
    it = iter(sequence)
    a = -1
    group = []
    while True:
        try:
            a, last = next(it), a
        except StopIteration:
            if group:
                yield group
            return
        if a < 0 and last >= 0:
            if group:
                yield group
            group = [a]
        else:
            group.append(a)

>>> sequence = [-2323, -2324, -53434, -1027, -34232, 343434, 5657, 6565, 6500, -343434, -3434, -565, 5845, 4667, 5453, 98356]
>>> list(takeSection(sequence))
Out[2]: 
[[-2323, -2324, -53434, -1027, -34232, 343434, 5657, 6565, 6500],
 [-343434, -3434, -565, 5845, 4667, 5453, 98356]]

Edit 编辑

If you want to filter this on the first value in a pair of values, you can change the if condition to test for this instead. 如果要对一对值中的第一个值进行过滤,则可以更改if条件以进行测试。 You could for instance change the condition line to if a[0] < 0 and last[0] >=0 , and you'd also need to initialise a as a = (-1, -1) 例如,您可以将条件行更改if a[0] < 0 and last[0] >=0 ,并且还需要将a初始化为a = (-1, -1)

However I'd be tempted to make a more generalised and useful function instead. 但是,我很想做一个更通用和有用的功能。

def sections(sequence, key):
    it = iter(sequence)
    a = placeholder = object()
    group = []
    while True:
        try:
            a, last = next(it), a
        except StopIteration:
            if group:
                yield group
            return
        if last is not placeholder and key(a, last):
            if group:
                yield group
            group = [a]
        else:
            group.append(a)

>>> sequence = [(-2323, -7465), (-2324, -7687), (-53434, -1027), (-34232, 343434), (5657, 6565), (6500, 978987), (-343434, -987), (-3434, -565), (-98, -8798), (-89898, -898), (5845, 4667), (5453, 98356)]
>>> list(sections(sequence, key=lambda current, last: current[0] < 0 and last[0] >= 0))
Out[1]:
[[(-2323, -7465), (-2324, -7687), (-53434, -1027), (-34232, 343434), (5657, 6565), (6500, 978987)],
 [(-343434, -987), (-3434, -565), (-98, -8798), (-89898, -898), (5845, 4667), (5453, 98356)]]

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

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