这是我的输入数据:如何将矢量映射到地图,重复键值?
[[:a 1 2] [:a 3 4] [:a 5 6] [:b \a \b] [:b \c \d] [:b \e \f]]
我想这映射到以下几点:
{:a [[1 2] [3 4] [5 6]] :b [[\a \b] [\c \d] [\e \f]]}
这是我到目前为止有:
(defn- build-annotation-map [annotation & m]
(let [gff (first annotation)
remaining (rest annotation)
seqname (first gff)
current {seqname [(nth gff 3) (nth gff 4)]}]
(if (not (seq remaining))
m
(let [new-m (merge-maps current m)]
(apply build-annotation-map remaining new-m)))))
(defn- merge-maps [m & ms]
(apply merge-with conj
(when (first ms)
(reduce conj ;this is to avoid [1 2 [3 4 ... etc.
(map (fn [k] {k []}) (keys m))))
m ms))
上述方法产生:
{:a [[1 2] [[3 4] [5 6]]] :b [[\a \b] [[\c \d] [\e \f]]]}
在我看来很清楚,问题在于merge-maps
,特别是与传递给merge-with
(conj
)的函数有关,但是在将头撞了一会儿之后,我即将准备好有人帮助我。
我是新来的lisp,特别是clojure,所以我也很感谢没有具体解决这个问题的意见,还有我的风格,脑死亡构造等。谢谢!
解(足够接近,反正):
(group-by first [[:a 1 2] [:a 3 4] [:a 5 6] [:b \a \b] [:b \c \d] [:b \e \f]])
=> {:a [[:a 1 2] [:a 3 4] [:a 5 6]], :b [[:b \a \b] [:b \c \d] [:b \e \f]]}
感谢您的评论,它有很大的帮助。 – 2010-09-12 17:30:04