簡體   English   中英

在 clojure 中關聯 map 中的值

[英]Associating a value in map in clojure

我想在如下所示的 output 中添加一個 key-val 對,其中編號為 2,站點標簽為 co2。

{:correct? false,
 :training-countdown nil,
 :site-communication {0 [], 1 [], 2 [], 3 (0 1), 4 (3 2)},
 :site-label :t-<<:temperature-:humidity_i3>-:co2_i4>,
 :agreement 478/739,
 :breakdown
 {0
  {:training-countdown nil,
   :site-label :temperature,
   :agreement 1,
   :site-order 0,
   :transformed-confidence 0.6367035786260278,
   :confidence-ma 0.86235090749618,
   :raw-votes (0.17910252984637456 0.10219403555517105),
   :id 10001,
   :class 0,
   :raw-confidence 0.17910252984637456,
   :site-type :p-site,
   :quantile 139/1000,
   :confidence 0.6367035786260278,
   :process-time {:cpu-nano 0, :wall-nano 14500}},
  1
  {:training-countdown nil,
   :site-label :humidity,
   :agreement 1,
   :site-order 0,
   :transformed-confidence 0.7396347578595606,
   :confidence-ma 0.7673990631272951,
   :raw-votes (0.05875164784557538 0.020681676807254086),
   :id 10001,
   :class 0,
   :raw-confidence 0.05875164784557538,
   :site-type :p-site,
   :quantile 273/1000,
   :confidence 0.7396347578595606,
   :process-time {:cpu-nano 0, :wall-nano 26600}},
  2
  {:training-countdown nil,
   :site-label :co2,
   :agreement 1,
   :site-order 0,
   :transformed-confidence 0.6885013017580619,
   :confidence-ma 0.8687745984384241,
   :raw-votes (6.209281474901374E-4 2.809265707284352E-4),
   :id 10001,
   :class 0,
   :raw-confidence 6.209281474901374E-4,
   :site-type :p-site,
   :quantile 79/1000,
   :confidence 0.6885013017580619,
   :process-time {:cpu-nano 0, :wall-nano 14400}},
  3
  {:training-countdown nil,
   :site-label :t-<:temperature-:humidity_i3>,
   :agreement 498/499,
   :site-order 1,
   :transformed-confidence 0.7146049092604565,
   :confidence-ma 0.9331555208607589,
   :raw-votes (220.42790579127143 551.9326320865079),
   :id 10001,
   :class 1,
   :raw-confidence 551.9326320865079,
   :site-type :t-site,
   :quantile 9/100,
   :confidence 0.7146049092604565,
   :process-time {:cpu-nano 0, :wall-nano 241300}},
  4
  {:training-countdown nil,
   :site-label :t-<<:temperature-:humidity_i3>-:co2_i4>,
   :agreement 478/739,
   :site-order 2,
   :transformed-confidence 0.932189769224312,
   :confidence-ma 0.845843308389129,
   :raw-votes (51.39818941403593 706.5728250787176),
   :id 10001,
   :class 1,
   :raw-confidence 706.5728250787176,
   :site-type :t-site,
   :quantile 317/500,
   :confidence 0.932189769224312,
   :process-time {:cpu-nano 0, :wall-nano 283700}}},
 :site-order 2,
 :transformed-confidence 0.932189769224312,
 :confidence-ma 0.845843308389129,
 :raw-votes (51.39818941403593 706.5728250787176),
 :id 10001,
 :class 1,
 :raw-confidence 706.5728250787176,
 :truth 0,
 :site-type :t-site,
 :quantile 317/500,
 :candidate-results
 {-1
  {:training-countdown nil,
   :site-label :humidity,
   :agreement 1,
   :site-order 0,
   :transformed-confidence 0.7396347578595606,
   :confidence-ma 0.7673990631272951,
   :raw-votes (0.05875164784557538 0.020681676807254086),
   :id 10001,
   :class 0,
   :raw-confidence 0.05875164784557538,
   :site-type :p-site,
   :quantile 273/1000,
   :confidence 0.7396347578595606,
   :process-time {:cpu-nano 0, :wall-nano 26600}},
  3
  {:training-countdown nil,
   :site-label :t-<:temperature-:humidity_i3>,
   :agreement 498/499,
   :site-order 1,
   :transformed-confidence 0.7146049092604565,
   :confidence-ma 0.9331555208607589,
   :raw-votes (220.42790579127143 551.9326320865079),
   :id 10001,
   :class 1,
   :raw-confidence 551.9326320865079,
   :site-type :t-site,
   :quantile 9/100,
   :confidence 0.7146049092604565,
   :process-time {:cpu-nano 0, :wall-nano 241300}},
  4
  {:training-countdown nil,
   :site-label :t-<<:temperature-:humidity_i3>-:co2_i4>,
   :agreement 478/739,
   :site-order 2,
   :transformed-confidence 0.932189769224312,
   :confidence-ma 0.845843308389129,
   :raw-votes (51.39818941403593 706.5728250787176),
   :id 10001,
   :class 1,
   :raw-confidence 706.5728250787176,
   :site-type :t-site,
   :quantile 317/500,
   :confidence 0.932189769224312,
   :process-time {:cpu-nano 0, :wall-nano 283700}}},
 :confidence 0.932189769224312,
 :process-time {:cpu-nano 0, :wall-nano 283700}}

您正在尋找的 function 是assoc-in ,它需要一個 map,一系列鍵來遍歷嵌套的 map 並由鍵形成的路徑結束。

例子:

user=> (def measures0 {:items {0 {:label "original label" :comment "nice"} 1 {:label "label 1" :comment "nice 1"}}})
#'user/measures0

user=> (clojure.pprint/pprint measures0)
{:items
 {0 {:label "original label", :comment "nice"},
  1 {:label "label 1", :comment "nice 1"}}}
nil

user=> (def measures-updated (assoc-in measures0 [:items 0 :label] "UPDATED LABEL"))
#'user/measures-updated

user=> (clojure.pprint/pprint measures-updated)
{:items
 {0 {:label "UPDATED LABEL", :comment "nice"},
  1 {:label "label 1", :comment "nice 1"}}}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM