4

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

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

我希望输出为:

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

5 回答 5

8

好的,这是一种方法。

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关联的所需结构:
    • grouping是键映射拆分中的唯一条目,以便可以将其重新排序为所需的形式
    • prices使用非常方便的方式完成,inject这与 Groovy 的fold left函数式语言操作等效
  • sort用于按照您指定的顺序翻转订单 - 必须质疑实际逻辑是什么,因此您可能想要替换它
  • 最后collect做最后的地图分配以获得确切的想要的形式

是的,它有点密集和神奇,但你总是可以将闭包移动到具有正确、描述性名称的 defs 中。

于 2013-10-12T09:48:23.710 回答
3

[编辑:现在根据原始问题返回地图列表]

受到 Vamsi Krishna 的回答的影响,但带有链式withDefault

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

def marketCommodityMap = [:].withDefault{ [:].withDefault{ [:].withDefault {[]} } }

// map looks like
// ["Terra-Wheat": ["Terra": ["Wheat": [1000.0 ...], "Barley": [5000.0] ]]]
// but we will discard the outer compound key                            
marketCommodityGroup.each { market, commodity, price ->    
    marketCommodityMap["${market}-${commodity}"][market][commodity] << price
}

def listOfMaps = marketCommodityMap.values()
println listOfMaps
于 2013-10-12T18:21:13.197 回答
3

另一个版本只是为了好玩;-)

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

def group( tree, data ) {
    if( data.size() > 2 ) { group( tree."${data.head()}", data.tail() ) }
    else {
        if( !tree."${data.head()}" ) tree."${data.head()}" = []
        tree."${data.head()}" << data[ -1 ]
    }
}

def grouped = { [:].withDefault{ owner.call() } }()
marketCommodityGroup.each {
    group( grouped, it )
}

assert grouped == ['Terra':['Wheat':[1000.0, 1000.0, 1500.0], 'Barley':[5000.0]],
                   'Shola':['Wheat':[1875.0]]]
于 2013-10-14T12:07:55.400 回答
1
def expected = [
   ["Terra": ["Wheat" : [1000.0, 1000.0, 1500.0]]],
   ["Terra": ["Barley": [5000.0]]],
   ["Shola": ["Wheat": [1875.0]]]
]                         

assert expected == marketCommodityGroup
                       .groupBy([{it[0]}, {it[1]}]) //Grouping based on 2 keys
                       .collectEntries{key, val-> 
                            [key, val.collectEntries{k, v -> //Collect entries
                                [k, v.collect{it[2]}.sort()]}] //Sorted price
}.inject([]){list, key, val -> //To transform result as a list
    val.each{k, v -> list << [(key): [(k): v]]}
    list
}

If you only need the map representation of the data then do not use inject, you would get:

[
 'Terra':['Wheat':[1000.0, 1000.0, 1500.0], 'Barley':[5000.0]], 
 'Shola':['Wheat':[1875.0]]
]
于 2013-10-12T20:18:44.413 回答
1

这可能会有所帮助:

def marketCommodityGroup = [["Terra", "Wheat", 1000.0],  
                            ["Shola", "Wheat", 1875.0],  
                            ["Terra", "Barely", 5000.0],  
                            ["Terra", "Wheat", 1000.0],  
                            ["Terra", "Wheat", 1500.0]]


def marketMap = [:].withDefault{[:]}  
def commodityMap = [:].withDefault {[]}

marketCommodityGroup.each{ market, comm, price ->
  commodityMap[comm].add(price)
}
println("commodityMap: "+commodityMap)                            

marketCommodityGroup.each{ market, comm, price ->
  marketMap[market][comm] = commodityMap[comm]
}                            
println("MarketMap: "+marketMap)

输出:

commodityMap: [Wheat:[1000.0, 1875.0, 1000.0, 1500.0], Barely:[5000.0]]
MarketMap: [Terra:[Wheat:[1000.0, 1875.0, 1000.0, 1500.0], Barely:[5000.0]], Shola:[Wheat:[1000.0, 1875.0, 1000.0, 1500.0]]]
于 2013-10-12T09:32:45.330 回答