【发布时间】:2017-10-27 03:33:31
【问题描述】:
给定以下字符串:
(def text "this is the first sentence . And this is the second sentence")
我想通过在每个单词出现后附加计数来计算文本中出现“this”之类的单词的次数。像这样:
["this: 1", "is" "the" "first" "sentence" "." "and" "this: 2" ...]
作为第一步,我对字符串进行了标记:
(def words (split text #" "))
然后我创建了一个辅助函数来获取“this”在文本中出现的次数:
(defn count-this [x] (count(re-seq #"this" text)))
最后我尝试在这个循环中使用count-this函数的结果:
(for [x words]
(if (= x "this")
(str "this: "(apply str (take (count-this)(iterate inc 0))))
x))
这是我得到的:
("this: 01" "is" "the" "first" "sentence" "." "And" "this: 01" "is" ...)
【问题讨论】:
-
因为没有答案使用
clojure.core/frequencies- 这里是doc
标签: string vector clojure count