gpt4 book ai didi

sorting - 如何使用无痛脚本语言从列表列表中区分行?

转载 作者:行者123 更新时间:2023-12-04 03:09:10 24 4
gpt4 key购买 nike

我有一个 Groovy 脚本:

def results = []
def cluster = ['cluster1', 'cluster1', 'cluster1', 'cluster1', 'cluster1', 'cluster1'];
def ports = ['4344', '4344', '4344', '4344', '4344', '4344'];
def hostname = [ 'cluster1.com','cluster1.com','cluster1.com','cluster1.com','cluster1.com','cluster1.com' ];

def heapu = ['533.6', '526.72' , '518.82' , '515.73', '525.69', '517.71'] ;
def heapm = ['1212.15', '1212.15', '1212.15', '1212.15', '1212.15', '1212.15'];
def times = ['2017-10-08T07:26:21.050Z', '2017-10-08T07:26:11.042Z', '2017-10-08T07:25:51.047Z', '2017-10-08T07:25:31.055Z', '2017-10-08T07:26:01.047Z', '2017-10-08T07:25:41.041Z'] ;

for (int i = 0; i < cluster.size(); ++i){
def c = cluster[i]
def p = ports[i]
def h = hostname[i]
def hu = heapu[i]
def hm = heapm[i]
def t = times[i]

results.add(['cluster': c,
'port': p,
'hostname': h,
'heap_used': hu,
'heap_max': hm,
'times': t])
results = results.unique()
}
// return ['results': results, 'singlex': singlex]

for (i = 0; i < results.size(); i++){
println(results[i])
}

此脚本的输出如下所示:

[cluster:cluster1, port:4344, hostname:cluster1.com, heap_used:533.6, heap_max:1212.15, times:2017-10-08T07:26:21.050Z]
[cluster:cluster1, port:4344, hostname:cluster1.com, heap_used:526.72, heap_max:1212.15, times:2017-10-08T07:26:11.042Z]
[cluster:cluster1, port:4344, hostname:cluster1.com, heap_used:518.82, heap_max:1212.15, times:2017-10-08T07:25:51.047Z]
[cluster:cluster1, port:4344, hostname:cluster1.com, heap_used:515.73, heap_max:1212.15, times:2017-10-08T07:25:31.055Z]
[cluster:cluster1, port:4344, hostname:cluster1.com, heap_used:525.69, heap_max:1212.15, times:2017-10-08T07:26:01.047Z]
[cluster:cluster1, port:4344, hostname:cluster1.com, heap_used:517.71, heap_max:1212.15, times:2017-10-08T07:25:41.041Z]

从输出中可以看出 -> 我基本上有 6 条相同的行,但时间戳不同。 HeapSize 和 Max HeapSize 不同,但这并不重要。

因为 cluster 对于 all 六个条目/cluster1/我认为它是一个输出。理想情况下,我想应用某种 unique() 函数,它会为我提供一行作为输出

喜欢以下内容:

[cluster:cluster1, port:4344, hostname:cluster1.com, heap_used:523.0450, heap_max:1212.15, times:2017-10-08T07:25:41.041Z]

其中 heap_used 是 6 个值的平均值以及 heap_max。我知道在 python pandas 中我可以用一个命令来完成它。但是我对 groovy 一无所知,我一直在互联网上搜索。

编辑:不幸的是,Groovy 解决方案不会将 1:1 转换为 Painless。

最佳答案

您可以按以下方式处理您的结果列表:

def grouped = results.groupBy { [it.cluster, it.port, it.hostname] }
.entrySet()
.collect { it -> [cluster: it.key.get(0), port: it.key.get(1), hostname: it.key.get(2)] + [
heap_used: it.value.heap_used*.toBigDecimal().sum() / it.value.size(),
heap_max: it.value.heap_max*.toBigDecimal().sum() / it.value.size(),
times: it.value.times.max()
]}

首先,我们按照包含clusterporthostname 的三元组对所有列表元素进行分组。然后我们通过将 clusterporthostnameheap_used: avg(heap_used) 组合来收集所有条目,heap_max:avg(heap_max)times:max(times)

这里

it.value.heap_used*.toBigDecimal().sum()

我们获取所有 heap_used 值的列表 (it.value.heap_used),然后我们使用扩展运算符应用 .toBigDecimal() 在每个列表元素上,因为您的初始值表示为字符串。为了计算平均值,我们只需将所有 heap_used 值的总和除以列表的大小。

输出

打印 grouped 变量将显示以下结果:

[[cluster:cluster1, port:4344, hostname:cluster1.com, heap_used:523.045, heap_max:1212.15, times:2017-10-08T07:26:21.050Z]]

关于sorting - 如何使用无痛脚本语言从列表列表中区分行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46634359/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com