交换地图中的键和值

mur*_*a52 15 clojure

是否有一个函数来交换给定地图的键和值.因此,给定一个地图,我希望键成为值,并为键值.

(swap {:a 2 b 4}) => {2 :a 4 :b}
Run Code Online (Sandbox Code Playgroud)

一种方法是

(zipmap (vals my-map) (keys my-map))
Run Code Online (Sandbox Code Playgroud)

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

A. *_*ebb 32

这是目的map-invertclojure.set:

user=> (clojure.set/map-invert {:a 2 :b 4})
{4 :b, 2 :a}
Run Code Online (Sandbox Code Playgroud)

  • 它确实共享了set不存储重复项的行为 - 只有任何给定val的最后一个成为一个键(其伴随键作为值) (5认同)
  • 有什么理由在clojure.set而不是clojure.core?我的意思是我需要注意的任何问题? (3认同)

beo*_*ver 6

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

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

被视为原子的值

一对一

地图的值是唯一的

(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}
Run Code Online (Sandbox Code Playgroud)

多对一

地图的值不是唯一的。这是很常见的 - 假设你的地图是这种形式是最安全的......所以(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
Run Code Online (Sandbox Code Playgroud)

被视为集合的值

一对多

值是集合/集合,但它们的交集始终为空。(没有元素出现在两个不同的集合中)

(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
Run Code Online (Sandbox Code Playgroud)

多对多

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

(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}
Run Code Online (Sandbox Code Playgroud)

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


ber*_*eal 5

reverse-mapclojure.contrib.datalog.util中有一个函数,它实现为:

(defn reverse-map
  "Reverse the keys/values of a map"
  [m]
  (into {} (map (fn [[k v]] [v k]) m)))
Run Code Online (Sandbox Code Playgroud)