矢量地图到地图矢量

时间:2014-05-19 19:12:32

标签: vector map clojure

我想替换这个结构,

{:a [1 2 .. 3] :b [1 2 .. 3] :c [1 2 3 ..]} 

到此。

[{:a 1 :b 1 c 1} {:a 2 :b 2 :c 2} {:a 3 :b 3 :c 3} ...] 

未指定键数。

4 个答案:

答案 0 :(得分:2)

我找到了解决方案,非常难看的解决方案,但它可以解释我需要的东西。

#(map (fn [line] 
       (zipmap (keys %) line)) 
     (partition (count (keys %)) 
                (apply interleave (vals %))))

如果有人能够简化这一点,那就太好了。

编辑:

#(map (fn [vs]
     (zipmap (keys %) vs))(apply map vector (vals %)))

答案 1 :(得分:0)

(def z (sorted-map :a [1 2 3 4] :b ["a" "b" "c" "d"] :c [:i :j :k :l]))

(defn x []
  (let [k (repeat (keys z))
        v (apply map vector (vals z))]
    (map zipmap k v)))


user> (x)
({:c :i, :b "a", :a 1} {:c :j, :b "b", :a 2} {:c :k, :b "c", :a 3} {:c :l, :b "d", :a 4})

答案 2 :(得分:0)

这是另一种解决方案

   #(mapv 
      (partial apply merge) 
      (map val 
         (group-by (comp  second first) 
         (for [[x y] % z y] (hash-map x z)))))

;=>[{:a 1, :c 1, :b 1} {:a 2, :c 2, :b 2} {:a 3, :c 3, :b 3}]

答案 3 :(得分:0)

(let [x  {1 [:a :b :c], 2 [:d :e :f], 3 [:g :h :i]}
      k (vec (keys x))
      r (map #(apply merge %)
             (apply map (fn [ & a]
                          (merge (map-indexed
                                  (fn [idx v] {(k idx) v}) a))) (vals x)))]
      (println r))