简体   繁体   English

Clojure - 将映射存储到键上的映射列表中

[英]Clojure - storing maps into a list of maps on keys

Say I have two maps in clojure.假设我在 clojure 中有两张地图。

(def map1 {:a 1 :b 1 :c nil :d 1})
(def map2 {:a 1 :b 2 :c 3 :d nil})

(def listofmaps '({:a 1 :b 1 :c nil :d 1} {:a 2 :b 2 :c 2 :d nil}))

If:a value matches with any map in listofmaps and map1, then if map1:d is not null, put:d value from map1 into the matching map in listofmaps. If:a value 与 listofmaps 和 map1 中的任意 map 匹配,则如果 map1:d 不是 null,则将 map1 中的 d 值放入 listofmaps 中匹配的 map。

Like, first we compare map1 and listofmaps - now if map1 (:a 1) matches with any maps in listofmaps (:a 1), if map1 (:d not null) replace (matching map in listofmaps with map1:d value) and if map1 (:c not null) replace (matching map in listofmaps with map1:c value)比如,首先我们比较 map1 和 listofmaps - 现在如果 map1 (:a 1) 与 listofmaps (:a 1) 中的任何地图匹配,如果 map1 (:d not null) 替换(匹配 listofmaps 中的 map 与 map1:d 值)和if map1 (:c not null) replace (用 map1:c 值匹配 listofmaps 中的 map)

    (def map1 {:a 1 :b 1 :c nil :d 1})
    (def listofmaps '({:a 1 :b 1 :c nil :d 1} {:a 2 :b 2 :c 2 :d nil}))

Then map2 (:a 1) matches with a map in listofmaps (:a 1) and:然后 map2 (:a 1) 与 listofmaps (:a 1) 中的 map 匹配,并且:

    (def map2 {:a 1 :b 2 :c 3 :d nil})
    (def listofmaps '({:a 1 :b 1 :c nil :d 1} {:a 2 :b 2 :c 2 :d nil}))

if map2 (:d not null) replace (matching map in listofmaps with map2:d value) and if map2 (:c not null) replace (matching map in listofmaps with map2:c value) if map2 (:d not null) replace (用 map2:d 值匹配 listofmaps 中的 map) if map2 (:c not null) replace (用 map2:c 值匹配 listofmaps 中的 map)

     output=> '({:a 1 :b 1 :c 3 :d 1} {:a 2 :b 2 :c 2 :d nil})

it's not clear what is meant by in list of maps and map2, here is a reasonably common pattern of adding in missing values in priority order.不清楚地图列表和 map2 中的含义,这是按优先顺序添加缺失值的合理常见模式。

(let [map1 {:a 1 :b 1 :c nil :d 1}
      map2 {:a 1 :b 2 :c 3 :d nil}
      list-of-maps [{:a 1 :b 1 :c nil :d 1} {:a 2 :b 2 :c 2 :d nil}]
      or-fn (fn [a b] (or a b))] 
 (->>
   list-of-maps
   (map #(merge-with or-fn % map1))
   (map #(merge-with or-fn % map2))))
({:a 1, :b 1, :c 3, :d 1} {:a 2, :b 2, :c 2, :d 1})

I understood your question to be我理解你的问题是

If the value of the :a key in the new map matches the value of the :a key in any map in listofmaps , then, for each such matched map, replace the values of the keys:c and:d in that matched map in listofmaps by a new value only if the corresponding new value is not null.如果新 map 中的 : :a键的值与 listofmaps 中任意listofmaps中的:a键的值相匹配,则对于每个这样匹配的 map,替换键的值:c 和:d 在匹配 map 中仅当相应的新值不是listofmaps时,才按新值列出 listofmaps。

Assuming that, here is an answer.假设,这是一个答案。 If I did not understand the question correctly, please clarify and show your desired output.如果我没有正确理解问题,请澄清并显示您想要的 output。

user> (def map1 {:a 1 :b 1 :c nil :d 1})
#'user/map1
user> (def map2 {:a 1 :b 2 :c 3 :d nil})
#'user/map2
user> (def listofmaps [{:a 1 :b 1 :c nil :d 1} {:a 2 :b 2 :c 2 :d nil}])
#'user/listofmaps
user> (defn mapper [m ms]
        (mapv (fn [elem]
                (if (= (:a elem) (:a m))
                  (merge-with #(or %1 %2) (select-keys m [:c :d]) elem)
                  elem))
              maps))
#'user/mapper
user> (mapper map1 listofmaps)
[{:c nil, :d 1, :a 1, :b 1} {:a 2, :b 2, :c 2, :d nil}]
user> (mapper map2 listofmaps)
[{:c 3, :d 1, :a 1, :b 1} {:a 2, :b 2, :c 2, :d nil}]
user> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM