简体   繁体   English

Clojure 从 zipmap 获取最高值

[英]Clojure getting highest value from zipmap

So I've got my proposed zip map here and it works perfectly.所以我在这里得到了我建议的 zip map,它工作得很好。 As you can see I've got the data loading.如您所见,我已经加载了数据。

That is what it looks like in the repl which is perfect.这就是它在完美的 repl 中的样子。 And right here is the map就在这里是 map

:Year 2020, :Day 27, :January 59, :February 38
:Year 2020, :Day 28, :January 41, :February 57
:Year 2020, :Day 29, :January 56, :February 51
:Year 2020, :Day 31, :January 94, :February -999
:Year 2020, :Day 30, :January 76, :February -999 

(map [:Day :Month

Bear in mind this is just a snippet of the code I've done.请记住,这只是我完成的代码片段。 How would you propose I find the highest value day in January?你会如何建议我找到一月份价值最高的一天? And by highest I mean by the number next to the months我所说的最高是指月份旁边的数字

(into(sorted-map-by >(fn [:January]))Ha) 

I tried this to no success, The "Ha" at the end is just the name of the function where I am initialising the zipmap and using io/reader to read the file我试过了没有成功,最后的“Ha”只是我初始化 zipmap 并使用 io/reader 读取文件的 function 的名称

Not sure how your exact datastructure looks like, but assuming it's a vector of maps you can do something like this:不确定你的确切数据结构是什么样子的,但假设它是一个映射向量,你可以这样做:

(def data
  [{:Year 2020, :Day 27, :January 59, :February 38}
   {:Year 2020, :Day 28, :January 41, :February 57}
   {:Year 2020, :Day 29, :January 56, :February 51}
   {:Year 2020, :Day 31, :January 94, :February -999}
   {:Year 2020, :Day 30, :January 76, :February -999}])

(->> data
     (sort-by :January)
     last
     :January)
;; => 94

Sorting the vector by using a keyword as a function to look up its value in the map, then taking the vector with the highest value for January, then get the value belonging to the key :January from that vector.通过使用关键字 function 对向量进行排序,以在 map 中查找其值,然后取具有最高值的向量一月,然后从该向量中获取属于键:January的值。 Let me know if your data structure looks a bit different.如果您的数据结构看起来有点不同,请告诉我。

I would use max-key and reduce :我会使用max-keyreduce

(def data [{:Year 2020, :Day 27, :January 59, :February 38}
           {:Year 2020, :Day 28, :January 41, :February 57}
           {:Year 2020, :Day 29, :January 56, :February 51}
           {:Year 2020, :Day 31, :January 94, :February -999}
           {:Year 2020, :Day 30, :January 76, :February -999}])

(reduce (partial max-key :January) data)
;; => {:Year 2020, :Day 31, :January 94, :February -999}

(:Day (reduce (partial max-key :January) data))
;; => 31

@Rulle's answer is very good. @Rulle 的回答很好。 Without max-key it would be:没有max-key它将是:

(def data [{:Year 2020, :Day 27, :January 59, :February 38}
           {:Year 2020, :Day 28, :January 41, :February 57}
           {:Year 2020, :Day 29, :January 56, :February 51}
           {:Year 2020, :Day 31, :January 94, :February -999}
           {:Year 2020, :Day 30, :January 76, :February -999}])

(defn seq-max [seq greater key]
  (reduce (fn [a b] (if (greater (key a) (key b)) a b)) seq))
;; if no key is wanted, choose the function `identity` as key!
;; e.g.
;; (seq-max (map :January data) > identity)
;; => 94

(:Day (seq-max data > :January)) ;; => 31

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

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