java - 如果标签相同则合并文件的时间戳

标签 java python algorithm groovy

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

输入1:

0.000000 0.551875 x 0.551875 0.586875 x 0.586875 0.676188 吨 0.676188 0.721875 吨 0.721875 0.821250 吨 0.821250 0.872063 磅 0.872063 0.968625 q 0.968625 1.112250 q

输入2:

0.000000 0.551875 x 0.551875 0.586875 x 0.586875 0.676188 吨 0.676188 0.721875 吨 0.721875 0.821250 吨 0.821250 0.872063 磅 0.872063 0.968625 q 0.968625 1.112250 q 1.112250 1.212250 x 1.212250 1.500000 x

输入3:

0.000000 0.551875 x 0.551875 0.586875 x 0.586875 0.676188 吨 0.676188 0.721875 吨 0.721875 0.821250 吨 0.821250 0.872063 哦 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 吨 0.821250 0.872063 磅 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']
]

只需将它们按每个列表中的第 3 个元素分组,然后为每个组创建一个列表,其中包含;

  • 第一个列表的第一项
  • 最后一个列表的第二项
  • 他们分组的关键

给予:

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']]

关于java - 如果标签相同则合并文件的时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39746043/

相关文章:

java - 迭代 Collection 或数组对象的常见方法

python - 在 Pandas 列中查找正则表达式模式

Python:有没有办法为输入字段设置默认值?

algorithm - 无向图中的最小割

java - 如何使用 PowerMockito 从最终静态类返回模拟对象

java - 类中的构造函数不能应用于给定类型android studio

java - android 用相机拍摄多张图像

python - cython编译错误 'is not a type identifier'

algorithm - 找到(稀疏)图直径的好算法?

arrays - 有没有比 O(N) 更好的算法来找到由连续 block 组成的位数组中的第一个位集?