我有以下地图列表
({"child.search" {:roles #{"ROLE_ADM_UNSUBSCRIBE_SUBSCRIPTION" "ROLE_ADM_SEARCH_SUBSCRIPTION" "ROLE_ADM_VIEW_SUBSCRIPTION"}},
"child.cc.search" {:roles #{"ROLE_ADM_CC_SEARCH_SUBSCRIPTION" "ROLE_ADM_CC_VIEW_SUBSCRIPTION"}}}
{"child.abusereport" {:roles #{"ROLE_ADM_ABUSE_RPT"}},
"child.manualfiltering" {:roles #{"ROLE_ADM_MANUAL_FILTERING_RPT"}}}
{"child.assigned.advertisement" {:roles #{"ROLE_ADM_CREATE_ADVERTISING"}},
"child.manage.advertisement" {:roles #{"ROLE_ADM_LIST_ADVERTISING"}}})
我需要有一个像下面这样的地图。
{"child.search" {:roles #{"ROLE_ADM_UNSUBSCRIBE_SUBSCRIPTION" "ROLE_ADM_SEARCH_SUBSCRIPTION" "ROLE_ADM_VIEW_SUBSCRIPTION"}}
"child.cc.search" {:roles #{"ROLE_ADM_CC_SEARCH_SUBSCRIPTION" "ROLE_ADM_CC_VIEW_SUBSCRIPTION"}}
"child.abusereport" {:roles #{"ROLE_ADM_ABUSE_RPT"}}
"child.manualfiltering" {:roles #{"ROLE_ADM_MANUAL_FILTERING_RPT"}}
"child.assigned.advertisement" {:roles #{"ROLE_ADM_CREATE_ADVERTISING"}}
"child.manage.advertisement" {:roles #{"ROLE_ADM_LIST_ADVERTISING"}}}
我该怎么做?
答案 0 :(得分:11)
您可以使用“ into ”功能,并提供空地图{}
作为第一个参数:
(into {} map-list)
这是我的REPL会话的输出;我将你的代码复制到两个vars'map-list'和'single-map'中(Clojure 1.3.0):
(def map-list '({"child.search" {:roles #{"ROLE_ADM_UNSUBSCRIBE_SUBSCRIPTION" "ROLE_ADM_SEARCH_SUBSCRIPTION" "ROLE_ADM_VIEW_SUBSCRIPTION"}},
"child.cc.search" {:roles #{"ROLE_ADM_CC_SEARCH_SUBSCRIPTION" "ROLE_ADM_CC_VIEW_SUBSCRIPTION"}}}
{"child.abusereport" {:roles #{"ROLE_ADM_ABUSE_RPT"}},
"child.manualfiltering" {:roles #{"ROLE_ADM_MANUAL_FILTERING_RPT"}}}
{"child.assigned.advertisement" {:roles #{"ROLE_ADM_CREATE_ADVERTISING"}},
"child.manage.advertisement" {:roles #{"ROLE_ADM_LIST_ADVERTISING"}}}))
#'user/map-list
user=>
(def single-map {"child.search" {:roles #{"ROLE_ADM_UNSUBSCRIBE_SUBSCRIPTION" "ROLE_ADM_SEARCH_SUBSCRIPTION" "ROLE_ADM_VIEW_SUBSCRIPTION"}}
"child.cc.search" {:roles #{"ROLE_ADM_CC_SEARCH_SUBSCRIPTION" "ROLE_ADM_CC_VIEW_SUBSCRIPTION"}}
"child.abusereport" {:roles #{"ROLE_ADM_ABUSE_RPT"}}
"child.manualfiltering" {:roles #{"ROLE_ADM_MANUAL_FILTERING_RPT"}}
"child.assigned.advertisement" {:roles #{"ROLE_ADM_CREATE_ADVERTISING"}}
"child.manage.advertisement" {:roles #{"ROLE_ADM_LIST_ADVERTISING"}}})
#'user/single-map
user=>
;; Check to see if we have the desired result
(= (into {} map-list)
single-map)
true
答案 1 :(得分:7)
我可能会使用merge
和apply
(def map-list (list {:a 1, :b 2} {:c 3, :d 4}))
(apply merge map-list) ;; returns {:a 1, :b 2, :c 3, :d 4}
答案 2 :(得分:2)
试试这个
(def your-list '({"child.search" {:roles #{"ROLE_ADM_UNSUBSCRIBE_SUBSCRIPTION" "ROLE_ADM_SEARCH_SUBSCRIPTION" "ROLE_ADM_VIEW_SUBSCRIPTION"}},
"child.cc.search" {:roles #{"ROLE_ADM_CC_SEARCH_SUBSCRIPTION" "ROLE_ADM_CC_VIEW_SUBSCRIPTION"}}}
{"child.abusereport" {:roles #{"ROLE_ADM_ABUSE_RPT"}},
"child.manualfiltering" {:roles #{"ROLE_ADM_MANUAL_FILTERING_RPT"}}}
{"child.assigned.advertisement" {:roles #{"ROLE_ADM_CREATE_ADVERTISING"}},
"child.manage.advertisement" {:roles #{"ROLE_ADM_LIST_ADVERTISING"}}}))
(reduce conj your-list)
答案 3 :(得分:0)
这个答案澄清了“进入”和“结合”在这种情况下相互关联的方式,以便为您的问题提供可接受的解决方案。也就是说,它站在前面答案的肩膀上。
您的列表格式为:
( {key1 value1}
{key2 value2} )
你想要一个已经提取了键/值对的地图如下:
{key1 value1, key2 value2}
问题在于,您基本上希望在单个地图中将每个连续的k / v对“联合”到最后一个。
上面的第一个解决方案使用了。如果我们看看clojuredoc进入,我们会看到:
“返回一个新的coll,它由to-coll与所有项目组成 来自coll-conjoined。“
另一个类似的答案是使用
(reduce conj '({key1 value1} {key2 value2}))
显然,这个解决方案正在解决上面针对这个问题的“into”定义:reduce函数累积第n +(第n + 1)个键值对的连接的每个应用程序,这样它实现了into的定义(至少,为了这个问题的目的)。