16

是否有交换给定地图的键和值的功能。所以给定一张地图,我希望键成为值,并为键赋值。

(swap {:a 2 b 4}) => {2 :a 4 :b}

一种方法是

(zipmap (vals my-map) (keys my-map))

但是想知道 clojure 是否为此提供了实用程序 fn ?

4

4 回答 4

34

这是map-invertin的目的clojure.set

user=> (clojure.set/map-invert {:a 2 :b 4})
{4 :b, 2 :a}
于 2013-03-24T12:43:17.503 回答
6

clojure.contrib.datalog.util 中有一个函数reverse-map,它的实现如下:

(defn reverse-map
  "Reverse the keys/values of a map"
  [m]
  (into {} (map (fn [[k v]] [v k]) m)))
于 2013-03-24T07:02:32.313 回答
6

对于以后阅读本文的任何人,我认为以下内容应该会有所帮助。

此处提供了一个小型库https://clojars.org/beoliver/map-inversions

反转地图可能会返回关系。如果映射是单射的(一对一),那么逆映射也是一对一的。如果地图(好像经常是这种情况)是多对一的,那么您应该使用集合或向量。

#Values 被视为原子 ##one-to-one map 的值是唯一的

(defn invert-one-to-one
  "returns a one-to-one mapping"
  [m]
  (persistent! (reduce (fn [m [k v]] (assoc! m v k)) (transient {}) m)))

(def one-to-one {:a 1 :b 2 :c 3})

> (invert-one-to-one one-to-one)
{1 :a 2 :b 3 :c}

##many-to-one map 的值不是唯一的。这是很常见的 - 假设你的地图是这种形式是最安全的......所以(def invert invert-many-to-one)

(defn invert-many-to-one
  "returns a one-to-many mapping"
  ([m] (invert-many-to-one #{} m))
  ([to m]
   (persistent!
    (reduce (fn [m [k v]]
              (assoc! m v (conj (get m v to) k)))
            (transient {}) m))))

(def many-to-one {:a 1 :b 1 :c 2})

> (invert-many-to-one many-to-one)
{1 #{:b :a}, 2 #{:c}} ; as expected

> (invert-many-to-one [] many-to-one)
{1 [:b :a], 2 [:c]} ; we can also use vectors

> (invert-one-to-one many-to-one) ; what happens when we use the 'wrong' function?
{1 :b, 2 :c} ; we have lost information

#Values 被视为集合##one-to-many 值是集合/集合,但它们的交集始终为空。(没有元素出现在两个不同的集合中)

(defn invert-one-to-many
  "returns a many-to-one mapping"
  [m]
  (persistent!
   (reduce (fn [m [k vs]] (reduce (fn [m v] (assoc! m v k)) m vs))
           (transient {}) m)))

(def one-to-many (invert-many-to-one many-to-one))
> one-to-many
{1 #{:b :a}, 2 #{:c}}

> (invert-one-to-many one-to-many)
{:b 1, :a 1, :c 2} ; notice that we don't need to return sets as vals

##many-to-many 值是集合/集合,并且至少存在两个交集不为空的值。如果您的值是集合,那么最好假设它们属于此类别。

(defn invert-many-to-many
  "returns a many-to-many mapping"
  ([m] (invert-many-to-many #{} m))
  ([to m]
   (persistent!
    (reduce (fn [m [k vs]]
              (reduce (fn [m v] (assoc! m v (conj (get m v to) k))) m vs))
            (transient {}) m))))

(def many-to-many {:a #{1 2} :b #{1 3} :c #{3 4}})

> (invert-many-to-many many-to-many)
{1 #{:b :a}, 2 #{:a}, 3 #{:c :b}, 4 #{:c}}

;; notice that there are no duplicates when we use a vector
;; this is because each key appears only once
> (invert-many-to-many [] many-to-many)
{1 [:a :b], 2 [:a], 3 [:b :c], 4 [:c]}

> (invert-many-to-one many-to-many)
{#{1 2} #{:a}, #{1 3} #{:b}, #{4 3} #{:c}}

> (invert-one-to-many many-to-many)
{1 :b, 2 :a, 3 :c, 4 :c}

> (invert-one-to-one many-to-many)
{#{1 2} :a, #{1 3} :b, #{4 3} :c} ; this would be missing information if we had another key :d mapping to say #{1 2}

您也可以invert-many-to-manyone-to-many示例中使用。

于 2017-07-02T17:46:59.767 回答
0

这是一个使用reduce可能适合问题的选项:

(reduce #(assoc %1 (second %2) (first %2)) {} {:a 2 :b 4})

这里在一个函数中

(defn invert [map]
  (reduce #(assoc %1 (second %2) (first %2)) {} map))

打电话

(invert {:a 2 b: 4})

然后是reduce-kv(我认为更清洁)

(reduce-kv #(assoc %1 %3 %2) {} {:a 2 :b 4})
于 2015-03-25T22:57:18.497 回答