如果标签相同,合并文件的时间戳



我的文件有三列,第一和第二列是时间的开始和结束,而第三列是标签。如果第三列中的标签相同,我想合并连续行(2或更多)的时间戳。

<标题> inputs1:

0.000000 0.551875 x 0.551875 0.586875 x 0.586875 0.676188 t 0.676188 0.721875 t 0.721875 0.821250 t 0.821250 0.872063 p 0.872063 0.968625 q 0.968625 1.112250 q

<标题> inputs2:

0.000000 0.551875 x 0.551875 0.586875 x 0.586875 0.676188 t 0.676188 0.721875 t 0.721875 0.821250 t 0.821250 0.872063 p 0.872063 0.968625 q 0.968625 1.112250 q 1.112250 1.212250 x 1.212250 1.500000 x

<标题> inputs3:

0.000000 0.551875 x 0.551875 0.586875 x 0.586875 0.676188 t 0.676188 0.721875 t 0.721875 0.821250 t 0.821250 0.872063 oo 0.872063 0.968625 q 0.968625 1.112250 q 1.112250 1.212250 x 1.212250 1.500000 x

<标题> 输出

0.000000 0.586875 x 0.586875 0.821250 t 0.821250 0.872063 p 0.872063 1.112250 q 1.112250 1.500000 x

在Groovy中,给定:

def inputs = [
    [0.000000, 0.551875, 'x'],
    [0.551875, 0.586875, 'x'],
    [0.586875, 0.676188, 't'],
    [0.676188, 0.721875, 't'],
    [0.721875, 0.821250, 't'],
    [0.821250, 0.872063, 'p'],
    [0.872063, 0.968625, 'q'],
    [0.968625, 1.112250, 'q']
]

按每个列表中的第三个元素分组,然后为每个组创建一个包含;

  • 第一个列表的第一项
  • 最后一个列表的第二项
  • 分组的关键字
给:

def outputs = inputs.groupBy { it[2] }.collect { key, items ->
    [items[0][0], items[-1][1], key]
}

结果是:

[[0.000000, 0.586875, 'x'],
 [0.586875, 0.821250, 't'],
 [0.821250, 0.872063, 'p'],
 [0.872063, 1.112250, 'q']]

的差距

如果您的输入可以有您想要保持的间隙,那么您可以尝试

def inputs = [[0.000000, 0.551875, 'x'],
              [0.551875, 0.586875, 'x'], 
              [0.586875, 0.676188, 't'], 
              [0.676188, 0.721875, 't'], 
              [0.721875, 0.821250, 't'], 
              [0.821250, 0.872063, 'p'], 
              [0.872063, 0.968625, 'q'], 
              [0.968625, 1.112250, 'q'], 
              [1.112250, 1.551875, 'x'], 
              [1.551875, 2.000000, 'x']]
def outputs = inputs.inject([]) { accum, line ->
    if(accum && accum[-1][2] == line[2]) {
        accum[-1][1] = line[1]
    }
    else {
        accum << line
    }
    accum
}

,

[[0.000000, 0.586875, 'x'],
 [0.586875, 0.821250, 't'],
 [0.821250, 0.872063, 'p'],
 [0.872063, 1.112250, 'q'],
 [1.112250, 2.000000, 'x']]

通配符

def inputs = [[0.000000, 0.551875, 'x'],
              [0.551875, 0.586875, 'x'], 
              [0.586875, 0.676188, 't'], 
              [0.676188, 0.721875, 't'], 
              [0.721875, 0.821250, 't'], 
              [0.821250, 0.872063, 'oo'], 
              [0.872063, 0.968625, 'q'], 
              [0.968625, 1.112250, 'q'], 
              [1.112250, 1.551875, 'x'], 
              [1.551875, 2.000000, 'x']]
def coalesce(List inputs, String... wildcards) {
    inputs.inject([]) { accum, line ->
        if(accum && 
           (accum[-1][2] == line[2] || wildcards.contains(line[2]))) {
            accum[-1][1] = line[1]
        }
        else {
            accum << line
        }
        accum
    }
}

,

def outputs = coalesce(inputs, 'oo')

给:

[[0.000000, 0.586875, 'x'],
 [0.586875, 0.872063, 't'],
 [0.872063, 1.112250, 'q'],
 [1.112250, 2.000000, 'x']]

def outputs = coalesce(inputs, 'oo', 'q')

[[0.000000, 0.586875, 'x'],
 [0.586875, 1.112250, 't'],
 [1.112250, 2.000000, 'x']]

最新更新