Dictionary clojure中带字母的映射索引
我试图在hashmap中映射字符串Dictionary clojure中带字母的映射索引,dictionary,clojure,hashmap,Dictionary,Clojure,Hashmap,我试图在hashmap中映射字符串txt中的字母索引,为此我尝试了 (let[ indices (map #(hash-map (keyword %1) %2) txt (range (count txt)))] 但我得到的是 ({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}
txt
中的字母索引,为此我尝试了
(let[
indices (map #(hash-map (keyword %1) %2) txt (range (count txt)))]
但我得到的是
({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 \!})
而我想要的是
({:0 \V} {:1 \a} ....
keyword
返回数值参数的nil
,因此需要首先将索引转换为字符串:
(map #(hash-map (keyword (str %1)) %2)
(range (count txt))
txt)
或者您可以使用地图索引
:
(map-indexed (fn [idx e] {(keyword (str idx)) e}) txt)
keyword
返回数值参数的nil
,因此需要首先将索引转换为字符串:
(map #(hash-map (keyword (str %1)) %2)
(range (count txt))
txt)
或者您可以使用地图索引
:
(map-indexed (fn [idx e] {(keyword (str idx)) e}) txt)
只是一个线索:
(映射索引哈希映射“asdfh”)
=>({0\a}{1\s}{2\d}{3\f}{4\h})和另一个线索:(zipmap(range)“aasjkb”)
{0\a,1\a,2\s,3\j,4\k,5\b}为什么要将数字转换为关键字?对,使用关键字就是问题所在。数字是很好的映射键,而在源文件中不以文本形式出现的关键字通常是愚蠢的。只是一条线索:(映射索引哈希映射“asdfh”)
=>({0\a}{1\s}{2\d}{3\f}{4\h})和另外一条线索:(zipmap(range)“aasjkb”)
{0\a,1\a,2\s,3\j,4\k,5\b}为什么要将数字转换为关键字?对,这里的问题是使用关键字。数字是很好的映射键,而在源文件中不以文本形式出现的关键字通常是愚蠢的。