简体   繁体   English

用clojure中的字母映射索引

[英]map indices with letters in clojure

I am trying to map indices of letters in a string txt in a hashmap, to do so I tried我正在尝试在哈希图中映射字符串txt中的字母索引,为此我尝试过

(let[
indices (map #(hash-map (keyword %1) %2) txt (range (count txt)))]

but what I get is something like但我得到的是类似的东西

({nil \V} {nil \a} {nil \d} {nil \e} {nil \r} {nil \space} {nil \s} {nil \a} {nil \i} {nil \d} {nil \:} {nil \space} {nil \N} {nil \o} {nil \,} {nil \space} {nil \I} {nil \space} {nil \a} {nil \m} {nil \space} {nil \y} {nil \o} {nil \u} {nil \r} {nil \space} {nil \f} {nil \a} {nil \t} {nil \h} {nil \e} {nil \r} {nil \!})

while what I want is而我想要的是

({:0 \V} {:1 \a} ....

keyword returns nil for numeric arguments so you need to convert the indices into strings first: keyword为数字参数返回nil ,因此您需要先将索引转换为字符串:

(map #(hash-map (keyword (str %1)) %2)
  (range (count txt))
  txt)

or you can use map-indexed :或者您可以使用map-indexed

(map-indexed (fn [idx e] {(keyword (str idx)) e}) txt)

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

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