gpt4 book ai didi

list - Clojure 将多张 map 合并为一张 map

转载 作者:行者123 更新时间:2023-12-03 23:28:46 25 4
gpt4 key购买 nike

我有以下 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"}}})

我需要一张像下面这样的 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"}}}

我该怎么做?

最佳答案

您可以使用“ into ”函数,并提供一个空 map {}作为第一个参数:

(into {} map-list)

这是我的 REPL session 的输出;我将您的代码复制到两个变量“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

关于list - Clojure 将多张 map 合并为一张 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9545778/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com