2017-07-12 101 views
1

我對Clojure很陌生,所以請耐心等待。我一直在爲這個看似簡單的任務而努力,這個任務是將地圖貼圖進行子集化幾個小時,然後我被困住了。在clojure中設置地圖的地圖

我輸入「類型」是一個字符串「在地圖」的集合,並且具有類似的形式:

{2 {3.507276 [1 "Mp2H"], 14.4985235 [1 "MpHpNa"], 21.541099 [1 "MpHpNH4"], 22.4854915 [1 "MpHpK"], 24.020551 [1 "MpACNp2H"], 25.489771 [1 "Mp2Na"], 33.0231235 [1 "2Mp3H2Op2H"], 44.533826 [1 "Mp2ACNp2H"], 65.047101 [1 "Mp3ACNp2H"]}, 1 {6.007276 [1 "MpH"], 11.007276000000001 [1 "2MpH"], 23.033823 [1 "MpNH4"], 27.989771 [1 "MpNa"], 28.033823 [1 "2MpNH4"], 32.989771000000005 [1 "2MpNa"], 38.033489 [1 "MpCH3OHpH"], 43.963707 [1 "MpK"], 47.033826 [1 "MpACNpH"], 48.963707 [1 "2MpK"], 52.033826 [1 "2MpACNpH"], 69.016321 [1 "MpACNpNa"], 74.016321 [1 "2MpACNpNa"], 88.060376 [1 "Mp2ACNpH"]}, 3 {2.673942666666667 [1 "Mp3H"], 10.001440966666667 [1 "Mp2HpNa"], 17.328939336666668 [1 "Mp2NaH"], 17.328943366666667 [1 "MpHp2Na"], 24.65643766666667 [1 "Mp3Na"], 27.978229666666667 [1 "Mp2KpH"]}}  

我想「在地圖」子集,只有那些內部項目匹配「類型的「

這樣的,如果我們的 '類型' 是["Mp2H" "MpH" "MpNH4" "Mp3H"]則回覆: {2 {3.507276 [1 "Mp2H"]}, 1{6.007276 [1 "MpH"], 23.033823 [1 "MpNH4"]}, 3{2.673942666666667 [1 "Mp3H"]}}

這是我目前的嘗試:

(map (fn [[id multiple-items]] 
    [id (->> multiple-items 
       (filter (fn [[_ single-type]] 
       (in? types (get single-type 1))) 
       ))] 
in-map)) 

目前這是返回object[clojure.core$map$fn__4549 0x47257bf1 "[email protected]"]。我有一個較早的嘗試是致力於子集,但輸出的格式並沒有看起來的輸入相匹配如下:

(map (fn [[id multiple-items]] 
     [id (filter (fn [[_ single-type]] 
       (in? types (get mol-adduct 1))) 
        multiple-items)]) 
in-map)) 

這將返回:

([2 ([3.507276 [1 "Mp2H"]])] [1 ([6.007276 [1 "MpH"]] [23.033823 [1 "MpNH4"]])] [3 ([2.673942666666667 [1 "Mp3H"]])]) 

這是不相同的格式輸入。

謝謝你的幫助!

回答

1

我能夠找出一些額外的Google Fu並玩弄!解決方案:

(into {} 
    (map (fn [[id multiple-items]] 
    [id (into {} 
      (filter (fn [[_ single-type]] 
        (in? types (get mol-adduct 1))) 
      multiple-items))]) 
    in-map))