Clojure:地图中的聚合和计数

时间:2016-03-21 19:15:07

标签: clojure monger

我猜这个问题有资格作为入门级的clojure问题。我基本上遇到了多次处理clojure地图的麻烦并提取不同类型的数据。

鉴于这样的地图,我试图根据多个嵌套键来计算条目:

.ext-44j {....}

首先,我想通过a键值对条目进行分组:

0

其次,我想假设b键的值是重复的,并忽略其余的键:

[
  {
    "a": "X",
    "b": "M",
    "c": 188
  },
  {
    "a": "Y",
    "b": "M",
    "c": 165
  },
  {
    "a": "Y",
    "b": "M",
    "c": 313
  },
  {
    "a": "Y",
    "b": "P",
    "c": 188
  }
]

然后,只需计算b键的所有实例:

{
  "X" : [
    {
      "b": "M",
      "c": 188
    }
  ],
  "Y" : [
    {
      "b": "M",
      "c": 165
    },
    {
      "b": "M",
      "c": 313
    },
    {
      "b": "P",
      "c": 188
    }
  ]
}

当我通过monger获取数据时,我定义了:

{
  "X" : [
    {
      "b": "M"
    }
  ],
  "Y" : [
    {
      "b": "M"
    }
    {
      "b": "P"
    }
  ]
}

然后点击路障:

{
  "X" : 1,
  "Y" : 2
}

我怎么能从这里继续?

3 个答案:

答案 0 :(得分:4)

这是一种天真的方法,我相信有更好的方法,但它可能是您需要弄清楚的。

(into {}
  (map       

    ; f       
    (fn [ [k vs] ] ;[k `unique count`]
      [k (count (into #{} (map #(get % "b") vs)))]) 

    ; coll
    (group-by #(get % "a") DATA))) ; "a"s as keys
;user=> {"X" 1, "Y" 2}

说明:

; I am using your literal data as DATA, just removed the , and ;
(def DATA [{...

(group-by #(get % "a") DATA) ; groups by "a" as keys
; so I get a map {"X":[{},...] "Y":[{},{},{},...]}

; then I map over each [k v] pair where
; k is the map key and
; vs are the grouped maps in a vector
(fn [ [k vs] ] 
      ; here `k` is e.g. "Y" and `vs` are the maps {a _, b, _, c _}

      ; now `(map #(get % "b") vs)` gets me all the b values
      ; `into set` makes them uniqe
      ; `count` counts them
      ; finally I return a vector with the same name `k`,
      ;   but the value is the counted `b`s
      [k (count (into #{} (map #(get % "b") vs)))]) 

; at the end I just put the result `[ ["Y" 2] ["X" 1] ]` `into` a map {}
; so you get a map

答案 1 :(得分:2)

(def data [{"a" "X", "b" "M", "c" 188}
       {"a" "Y", "b" "M", "c" 165}
       {"a" "Y", "b" "M", "c" 313}
       {"a" "Y", "b" "P", "c" 188}])
;; Borrowing data from @leetwinski

如果要定义数据,可能需要考虑的一件事是使用关键字而不是字符串作为键。这样做的好处是能够使用关键字作为访问地图中的内容的函数,即(get my-map "a")变为(:a my-map)

要按“a”键分组数据:

(defn by-a-key [data] 
  (group-by #(get % "a") data))

我认为你实际上可以跳过你的第二步,如果只是为了让你进入你的第三步,因为这样做是不需要的。在第二次阅读时,我无法判断您是否只想为每个不同的“b”键保留一个元素。我将假设没有,因为你没有指定如何选择保留哪些,它们看起来大不相同。

(reduce-kv 
  (fn [m k v] 
    (assoc m k 
      (count (filter #(contains? % "b") v)))) 
  {} 
  (by-a-key data))

你也可以这样做:

(frequencies (map #(get % "a") (filter #(contains? % "b") data)))

由于您可以在分组前按“b”键进行过滤,因此可以依靠频率对您进行分组和计数。

答案 2 :(得分:1)

您可以使用reduce

进行制作
(def data [{"a" "X", "b" "M", "c" 188}
           {"a" "Y", "b" "M", "c" 165}
           {"a" "Y", "b" "M", "c" 313}
           {"a" "Y", "b" "P", "c" 188}])

(def processed (reduce #(update % (%2 "a") (fnil conj #{}) (%2 "b")) 
                       {} data))

;; {"X" #{"M"}, "Y" #{"M" "P"}}
;; you create a map of "a" values to a sets of "b" values in one pass
;; and then you just create a new map with counts

(reduce-kv #(assoc %1 %2 (count %3)) {} processed)

;; {"X" 1, "Y" 2}

因此它使用与@ birdspider解决方案相同的逻辑,但使用较少的集合

在一个函数中:

(defn process [data]
  (->> data
       (reduce #(update % (%2 "a") (fnil conj #{}) (%2 "b")) {})
       (reduce-kv #(assoc %1 %2 (count %3)) {})))

 user> (process data)
 ;; {"X" 1, "Y" 2}