Is it possible to "rearrange" a list of cards into a map of lists in Clojure?

Hi huys: I want to display "average" for all values โ€‹โ€‹on the map. I have a list of maps:

[{"age" 2 "height" 1 "weight" 10},
{"age" 4 "height" 4 "weight" 20},
{"age" 7 "height" 11 "weight" 40}]

And my desired result

{"age 5 "height" 5 ....}

/// The following are the breaks of my brain, that is, the way I can imagine that this works in Clojure ... is not taken too seriously

transpose the list:

  {"age" [2 4 7] "height" [1 4 11] } 

and then I could just do something like (again, by creating a function called freduce here)

  (freduce average (vals (map key-join list)))

To obtain

{"age" 5 "weight" 10 "height" 7}

+5
source share
6 answers

Here's a pretty verbose solution. Hope someone can come up with something better:

(let [maps [{"age" 2 "height" 1 "weight" 10},
            {"age" 4 "height" 4 "weight" 20},
            {"age" 7 "height" 11 "weight" 40}]
      ks (keys (first maps))
      series-size (count maps)
      avgs (for [k ks]
             (/ (reduce +
                        (for [m maps]
                          (get m k)))
                series-size))]
  (zipmap ks avgs))
+4
source

Create a vector map:

(reduce (fn [m [kv]]
          (assoc m k (conj (get m k []) v)))
        {}
        (apply concat list-of-maps))

:

(reduce (fn [m [k v]]
          (assoc m k (/ (reduce + v) (count v))))
        {}
        map-of-vectors)
+6

Take a look at merge-with

Here is my code:

(let [maps [{"age" 2 "height" 1 "weight" 10},
            {"age" 4 "height" 4 "weight" 20},
            {"age" 7 "height" 11 "weight" 40}]]
  (->> (apply merge-with #(conj %1 %2)
             (zipmap (apply clojure.set/union (map keys maps))
                     (repeat [])) ; set the accumulator
             maps)
       (map (fn [[k v]] [k (/ (reduce + v) (count v))]))
       (into {})))
+4
source
(defn key-join [map-list]
  (let [keys (keys (first map-list))]
       (into {} (for [k keys] [k (map #(% k) map-list)]))))
(defn mapf [f map]
  (into {} (for [[k v] map ] [k (f v)])))
(defn average [col]
  (let [n (count col)
        sum (apply + col)]
       (/ sum n)))

Demo

user=> (def data-list [{"age" 2 "height" 1 "weight" 10},
{"age" 4 "height" 4 "weight" 20},
{"age" 7 "height" 11 "weight" 40}])
#'user/data-list
user=> (key-join data-list)
{"age" (2 4 7), "height" (1 4 11), "weight" (10 20 40)}
user=> (mapf average (key-join data-list))
{"age" 13/3, "height" 16/3, "weight" 70/3}
+2
source

Here's another version that uses merging - no zipmap.

(let [data [{:a 1 :b 2} {:a 2 :b 4} {:a 4 :b 8}]
           num-vals (count data)]
     (->> data (apply merge-with +) 
          (reduce (fn [m [k v]] (assoc m k (/ v num-vals))) {})))
+1
source

Here is my one line solution:

(def d [{"age" 2 "height" 1 "weight" 10},
   {"age" 4 "height" 4 "weight" 20},
   {"age" 7 "height" 11 "weight" 40}])

(into {} (map (fn [[k v] [k (/ v (count d))]]) (apply merge-with + d)))

=> {"height" 16/3, "weight" 70/3, "age" 13/3}

The logic is as follows:

  • Use merge-with + on cards to calculate the sum for each key value
  • Divide all the values โ€‹โ€‹obtained by the total number of cards to get the average value
  • Return the results to the hash map using (in {} ...)
+1
source

All Articles