如何根据唯一值映射clojure中2组中的不同值

时间:2016-09-26 21:11:15

标签: clojure

我有一个提供数据的功能A

{{id 1,:obs/A "11", :obs/value 2.0, :obs/color "yellow"}
{id 2,:obs/A "12", :obs/value 4.0, :obs/color "blue"}
{id 3,:obs/A "13", :obs/value 3.0, :obs/color "green"}
{id 3,:obs/A "15", :obs/value 7.0, :obs/color "red"}...}

and a function B which gives the data

{{id 2,:obs/A "11", :obs/value 7.0, :obs/shape "square"}
{id 2,:obs/A "13", :obs/value 4.0, :obs/shape "circle"}
{id 6,:obs/A "15", :obs/value 3.0, :obs/shape "triangle"}...}

I want to map obs/value from both functions which match with same obs/A.

Here the result will be like {(2.0,7.0),(3.0,4.0)..}

我正在使用过滤器功能和地图,但无法获得正确的代码。

谢谢。

5 个答案:

答案 0 :(得分:0)

更新2016-9-26 1727:我添加了一个更好的解决方案,它使用DataScript来完成所有艰苦的工作。请在最后查看其他解决方案。

这是一个有效的答案(没有DataScript):

(ns clj.core
  (:require [tupelo.core :as t] 
            [clojure.set :as set] ))
(t/refer-tupelo)

(def x
  [ {:id 1,   :obs/A "11",    :obs/value 2.0,    :obs/color "yellow"}
    {:id 2,   :obs/A "12",    :obs/value 4.0,    :obs/color "blue"}
    {:id 3,   :obs/A "13",    :obs/value 3.0,    :obs/color "green"}
    {:id 3,   :obs/A "15",    :obs/value 7.0,    :obs/color "red"} ] )

(def y
  [ {:id 2,   :obs/A "11",    :obs/value 7.0,    :obs/shape "square"}
    {:id 2,   :obs/A "13",    :obs/value 4.0,    :obs/shape "circle"}
    {:id 6,   :obs/A "15",    :obs/value 3.0,    :obs/shape "triangle"} ] )

(newline) (println "x") (pretty x)
(newline) (println "y") (pretty y)

; Note this assumes that :obs/A is unique in each sequence x and y
(def xa (group-by :obs/A x))
(def ya (group-by :obs/A y))
(newline) (println "xa") (pretty xa)
(newline) (println "ya") (pretty ya)

(def common-a (set/intersection (set (keys xa)) (set (keys ya))))
(newline) (spyx common-a)

(def values-map
  (apply glue
    (for [aval common-a]
      { (-> aval xa only :obs/value)
        (-> aval ya only :obs/value) } )))
(newline) (spyx values-map)


> lein run
x
[{:id 1, :obs/A "11", :obs/value 2.0, :obs/color "yellow"}
 {:id 2, :obs/A "12", :obs/value 4.0, :obs/color "blue"}
 {:id 3, :obs/A "13", :obs/value 3.0, :obs/color "green"}
 {:id 3, :obs/A "15", :obs/value 7.0, :obs/color "red"}]

y
[{:id 2, :obs/A "11", :obs/value 7.0, :obs/shape "square"}
 {:id 2, :obs/A "13", :obs/value 4.0, :obs/shape "circle"}
 {:id 6, :obs/A "15", :obs/value 3.0, :obs/shape "triangle"}]

xa
{"11" [{:id 1, :obs/A "11", :obs/value 2.0, :obs/color "yellow"}],
 "12" [{:id 2, :obs/A "12", :obs/value 4.0, :obs/color "blue"}],
 "13" [{:id 3, :obs/A "13", :obs/value 3.0, :obs/color "green"}],
 "15" [{:id 3, :obs/A "15", :obs/value 7.0, :obs/color "red"}]}

ya
{"11" [{:id 2, :obs/A "11", :obs/value 7.0, :obs/shape "square"}],
 "13" [{:id 2, :obs/A "13", :obs/value 4.0, :obs/shape "circle"}],
 "15" [{:id 6, :obs/A "15", :obs/value 3.0, :obs/shape "triangle"}]}

common-a => #{"15" "13" "11"}

values-map => {7.0 3.0, 3.0 4.0, 2.0 7.0}

这就像制作一个迷你数据库并询问(sql伪代码):

select x.value, y.value from 
  (natural join x, y on A)

如果你这样做很多,你可能会发现使用像PostgreSQL或Datomic这样的真实数据库是有用的,或者对于仅限内存的东西,可以考虑使用clojure lib DataScript。

以下是DataScript的答案:

(ns clj.core
  (:require [tupelo.core :as t] 
            [datascript.core :as d]
            [clojure.set :as set] ))
(t/refer-tupelo)

(def data
  [ {:type :x :local/id 1,   :obs/A "11",    :obs/value 2.0,    :obs/color "yellow"}
    {:type :x :local/id 2,   :obs/A "12",    :obs/value 4.0,    :obs/color "blue"}
    {:type :x :local/id 3,   :obs/A "13",    :obs/value 3.0,    :obs/color "green"}
    {:type :x :local/id 3,   :obs/A "15",    :obs/value 7.0,    :obs/color "red"} 

    {:type :y :local/id 2,   :obs/A "11",    :obs/value 7.0,    :obs/shape "square"}
    {:type :y :local/id 2,   :obs/A "13",    :obs/value 4.0,    :obs/shape "circle"}
    {:type :y :local/id 6,   :obs/A "15",    :obs/value 3.0,    :obs/shape "triangle"} ] )

(def conn (d/create-conn {}))
(d/transact! conn data)

(def labelled-result
  (d/q '[:find ?a ?value1 ?value2
             :where 
               [?ex :type :x] [?ex :obs/A ?a] [?ex :obs/value ?value1]
               [?ey :type :y] [?ey :obs/A ?a] [?ey :obs/value ?value2]
            ] @conn ))
(newline) (println "labelled-result") (pretty labelled-result)

(def unlabelled-result
  (d/q '[:find    ?value1 ?value2
             :where 
               [?ex :type :x] [?ex :obs/A ?a] [?ex :obs/value ?value1]
               [?ey :type :y] [?ey :obs/A ?a] [?ey :obs/value ?value2]
            ] @conn ))
(newline) (println "unlabelled-result") (pretty unlabelled-result)

> lein run

labelled-result
#{["13" 3.0 4.0] ["11" 2.0 7.0] ["15" 7.0 3.0]}

unlabelled-result
#{[3.0 4.0] [2.0 7.0] [7.0 3.0]}

答案 1 :(得分:0)

好的,我并非100%确定我已经掌握了你的问题,但是根据你所描述的,你有两个任意地图列表,你从所有的地方收集特定的元素映射到列表。使用其中一个合并(merge-fn,可能是?)可能是一个真正灵巧的方法,但是使用普通的旧的reduce,你可以这样做:

    (vals (reduce 
      (fn[acc i]
        (let [k (:key i)
              v (:value i)]
          (assoc acc k (conj (acc k) v)))) {} (concat a n)))

让我们仔细看看。从结尾开始:

(concat a n)

我连接列表是因为您已经表明它们是完全独立的地图列表。列表中没有唯一性,因此将它们全部视为一个列表可能会有所不同。

{}

我传了一张空地图。我们想要一张地图,因为在构建地图时,我们需要使用我们的首选密钥跟踪我们在哪里放置内容。为了减少,我传递了一个函数:

(fn[acc i]

它需要一个累加器和一个项目(分别是acc和i)。我们从i中取出密钥,这是我们的地图:

 (let [k (:key i)

我用过:键是为了清晰,但在你的例子中,你想要的是obs / A.然后我取值:

  v (:value i)]

然后我将该值与累加器中的键相关联,并将其与已存在的值相关联:

(assoc acc k (conj (acc k) v))))

这是一个很好的技巧:

(conj nil :whatever)

返回

'(whatever)

(conj '(:whatever) :something)

返回:

'(:whatever :something)

所以你不必为第一种情况做任何特别的事情。

当我们全部完成后,我们会有一张包含所有相关值的地图,就像我的情况一样:

(def a [{:key 1 :value 2}{:key 2 :value 3}])
(def n [{:key 1 :value 3}{:key 2 :value 4}])

所以,只有减少回报:

=> {1 (3 2), 2 (4 3)}

我们只想要地图的值,所以我们将它们全部包含在一个vals中并且瞧瞧:

'((3 2) (4 3))

希望有所帮助。

答案 2 :(得分:0)

如果你知道在同一个集合中没有相同:obs/A的项目,你可以将两个集合连接起来,在:obs/A上对它们进行分组,并保留值在2个项目中的值组:

user> (def rel1 #{{:id 1,:obs/A "11", :obs/value 2.0, :obs/color "yellow"}
                  {:id 2,:obs/A "12", :obs/value 4.0, :obs/color "blue"}
                  {:id 3,:obs/A "13", :obs/value 3.0, :obs/color "green"}
                  {:id 3,:obs/A "15", :obs/value 7.0, :obs/color "red"}})
#'user/rel1

user> (def rel2 #{{:id 2,:obs/A "11", :obs/value 7.0, :obs/shape "square"}
                  {:id 2,:obs/A "13", :obs/value 4.0, :obs/shape "circle"}
                  {:id 6,:obs/A "15", :obs/value 3.0, :obs/shape "triangle"}})
#'user/rel2

user> (keep (fn [[_ v]] (when (> (count v) 1) (map :obs/value v)))
            (group-by :obs/A (concat rel1 rel2)))
;;=> ((3.0 4.0) (7.0 3.0) (2.0 7.0))

否则您首先必须找到两个集合中存在的:obs/A值,然后找到相应的值:

user> (let [r1 (group-by :obs/A rel1)
            r2 (group-by :obs/A rel2)
            ;; keysets intersection
            ks (keep (set (keys r1)) (keys r2))]
        (map #(map :obs/value (concat (r1 %) (r2 %)))
             ks))
;;=> ((2.0 7.0) (7.0 3.0) (3.0 4.0))

答案 3 :(得分:0)

data是合并后的回复:

(into {} (for [[k v] (group-by :obs/A data)]
           (if (= 2 (count v))
             [k (map :obs/value v)])))

=> {"11" (2.0 7.0), "13" (3.0 4.0), "15" (7.0 3.0)}

如果您不想使用标签,请使用vals

答案 4 :(得分:0)

使用clojure.set

(def a #{{:id 1,:obs/A "11", :obs/value 2.0, :obs/color "yellow"}
        {:id 2,:obs/A "12", :obs/value 4.0, :obs/color "blue"}
        {:id 3,:obs/A "13", :obs/value 3.0, :obs/color "green"}
        {:id 3,:obs/A "15", :obs/value 7.0, :obs/color "red"}})

(def b #{{:id 2,:obs/A "11", :obs/value 7.0, :obs/shape "square"}
        {:id 2,:obs/A "13", :obs/value 4.0, :obs/shape "circle"}
        {:id 6,:obs/A "15", :obs/value 3.0, :obs/shape "triangle"}})

(use 'clojure.set)

(->> [a b]
    (map #(index % [:obs/A]))
    (apply merge-with union)
    vals
    (map (partial map :obs/value)))

答案:((3.0 4.0)(4.0)(3.0 7.0)(7.0 2.0))