如何使用groovy对列表进行分组 [英] How to group a list of list using groovy

查看:585
本文介绍了如何使用groovy对列表进行分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表,我想按照marketName和commodityName分组,列出价格:
例如[Terra,Wheat,1000.0] - Terra是市场名称,Wheat是商品名称,1000.0是价格

  def marketCommodityGroup = [
[Merkato,Wheat,1000.0],
[Shola,Wheat,1875.0],
[Merkato,Barley ,5000.0],
[Merkato,小麦,1000.0],
[Merkato,小麦,1500.0]
]

我希望输出为:

 <$ c $ 
[Merkato:[Wheat:[1000.0,1000.0,1500.0]]],
[Merkato:[Barley:[5000.0]]],
[Shola:[Wheat:[1875.0]]]
]


解决方案

好吧,这里有一个方法。

  def mapped = marketCommodityGroup.groupBy { 
[(it [0]):it [1]]
} .collect {k,v - >
def grouping = k.find {true}
def prices = v.inject([]){acc,val - > acc + val [2]}
[(grouping.key),[(grouping.value):prices]]
} .sort {left,right - >
right [0]< => left [0]
} .collect {
[(it [0]):it [1]]
}




  • 第一个 groupBy 确实 通过市场名称和商品名称
  • collect 创建除最终 k:v association:

    • grouping 是键映射拆分中的唯一条目,因此可以重新排序到期望的形式

    • 价格用非常方便的 inject 来完成,它是Groovy等同于折叠功能语言中的操作


  • sort 用于按照指定的顺序翻转订单 - 必须确定实际的逻辑是什么,以便您可以替换它

  • last collect 做最终的地图分配以获得确切的想要的表单



是的,它有点密集和神奇的,但你总是可以移动关闭def带有适当的描述性名称。

I have a list of list that I want to group by marketName and commodityName, by listing price: e.g. ["Terra", "Wheat", 1000.0] - "Terra" is Market name, "Wheat" is Commodity Name, 1000.0 is price

def marketCommodityGroup = [
                              ["Merkato", "Wheat", 1000.0],  
                              ["Shola", "Wheat", 1875.0],  
                              ["Merkato", "Barley", 5000.0],  
                              ["Merkato", "Wheat", 1000.0],  
                              ["Merkato", "Wheat", 1500.0] 
                           ] 

I would like the output to be:

[
   ["Merkato": ["Wheat" : [1000.0, 1000.0, 1500.0]]],
   ["Merkato": ["Barley": [5000.0]]],
   ["Shola": ["Wheat": [1875.0]]]
]

解决方案

Alright, here's one way of doing it.

def mapped = marketCommodityGroup.groupBy {
  [(it[0]) : it[1]]
}.collect { k,v ->
  def grouping = k.find { true }
  def prices = v.inject([]) { acc,val -> acc + val[2] }
  [ (grouping.key) , [ (grouping.value) : prices ] ]
}.sort { left, right ->
  right[0] <=> left[0]
}.collect {
  [(it[0]) : it[1] ]
}

  • first groupBy does exactly what you said, it groups by the market name and commodity name
  • collect creates the wanted structure excluding the final k:v association:
    • grouping is the only entry in the key map split so that it can be reordered to desired form
    • prices is done with the very handy inject which is Groovy's equivalent for the fold left operation in functional languages
  • sort is for flipping the order as you specified - had to quess what the actual logic is so you may want to replace it
  • last collect does the final map assignment to get the exact wanted form

Yes, it's a bit dense and magical but you can always move the closures to defs with proper, descriptive names.

这篇关于如何使用groovy对列表进行分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆