我创建了一个名为pretend
的地图列表。
(def pretend (map (fn [num] {:alias (str "alias " num), :real (str "real " num)}) (range 6 32)))
给出了:
({:alias "alias 6", :real "real 6"} {:alias "alias 7", :real "real 7"} {:alias "alias 8", :real "real 8"} {:alias "alias 9", :real "real 9"} {:alias "alias 10", :real "real 10"} {:alias "alias 11", :real "real 11"} {:alias "alias 12", :real "real 12"} {:alias "alias 13", :real "real 13"} {:alias "alias 14", :real "real 14"} {:alias "alias 15", :real "real 15"} {:alias "alias 16", :real "real 16"} {:alias "alias 17", :real "real 17"} {:alias "alias 18", :real "real 18"} {:alias "alias 19", :real "real 19"} {:alias "alias 20", :real "real 20"} {:alias "alias 21", :real "real 21"} {:alias "alias 22", :real "real 22"} {:alias "alias 23", :real "real 23"} {:alias "alias 24", :real "real 24"} {:alias "alias 25", :real "real 25"} {:alias "alias 26", :real "real 26"} {:alias "alias 27", :real "real 27"} {:alias "alias 28", :real "real 28"} {:alias "alias 29", :real "real 29"} {:alias "alias 30", :real "real 30"} {:alias "alias 31", :real "real 31"})
我想将列表中的每个元素插入到以下向量中。
(def identities
[{:alias "Batman" :real "Bruce Wayne"}
{:alias "Spiderman" :real "Peter Parker"}
{:alias "Santa" :real "Your mom"}
{:alias "Easter Bunny" :real "Your dad"}
{:alias "alias 5", :real "real 5"}
;; ... elements from the "pretend" list should be inserted here
{:alias "alias 31", :real "real 31"}
{:alias "alias 32", :real "real 32"}
{:alias "alias 33", :real "real 33"}
{:alias "alias 34", :real "real 34"}])
但是当我尝试以下操作时,我没有这样做。它用索引列表替换了索引5处的元素,这不是我想要的。我想将列表pretend
中的所有地图元素插入到索引为5的地图矢量中。
(def identities (assoc identities 5 pretend))
答案 0 :(得分:6)
(let [[before after] (split-at 5 identities)]
(vec (concat before pretend after)))
我对您的数据结构有两个顾虑。
def
进行增量更改。做两次
你会有一堆重复的条目。 看起来像这样:
{"Batman" "Bruce Wayne", "alias 5" "real 5", "Santa" "Your mom",
"alias 31" "real 31", "alias 32" "real 32", "alias 33" "real 33",
"Easter Bunny" "Your dad", "alias 34" "real 34", "Spiderman" "Peter Parker"}
您可以从identities
导出它:
(into {} (map (juxt :alias :real) identities))