如何反转值为序列的地图? 在反转映射中,键是所有序列的条目(假设没有重复键)
例如:
(map-invert-tweaked {:monday [:banana :apple]
:sunday [:pineapple :mango]}); {:banana :monday
:apple :monday
:pineapple :sunday
:mango :sunday}
我希望这个问题定义明确。
答案 0 :(得分:8)
你可以使用列表理解。像这样:
(defn map-invert-tweaked [m]
(into {} (for [[k vs] m
v vs]
[v k])))
这个for
部分做了类似的事情:
var result = [];
for (var k in m) {
var vs = m[k];
for (var i = 0; i < vs.length; i++) {
var v = vs[i]
result.push([v, k]);
}
}
然后它只是将所有这些对放入地图
在repl中:
user> (map-invert-tweaked {:monday [:banana :apple]
:sunday [:pineapple :mango]})
{:banana :monday, :apple :monday, :pineapple :sunday, :mango :sunday}
答案 1 :(得分:-1)
是的,上述方法有效,但使用实际单词作为变量名更容易阅读:
(defn invert-keylist-map [keylist-map]
(into {}
(for [ [curr-key value-list] keylist-map
curr-value value-list]
[curr-value curr-key] )))
更好的是使用Prismatic Schema来描述输入和输出的形状。像这样的输出:
(ns ...
(:require [schema.core :as s] ))
(s/defn invert-keylist-map :- { s/Any s/Any } ; result is a map of anything -> anything
[keylist-map :- { s/Any [s/Any] } ] ; input is a map of anything -> list-of-anything
(into {}
(for [ [curr-key value-list] keylist-map
curr-value value-list]
[curr-value curr-key] )))