简体   繁体   English

Clojure:如何在另一个向量中获取向量的元素

[英]Clojure: how to get an element of a vector in another vector

I am very new to Clojure and am having difficulties understanding the operations of vectors/lists/maps. 我对Clojure非常陌生,在理解向量/列表/地图的操作时遇到困难。 I am trying to print out the names of all the customers in data, but I cannot figure out how. 我正在尝试打印出数据中所有客户的姓名,但是我不知道该怎么做。 Please help. 请帮忙。

(def data
"1|John Smith|123 Here Street|456-4567
2|Sue Jones|43 Rose Court Street|345-7867
3|Fan Yuhong|165 Happy Lane|345-4533")

(defn test
[]
  (let [lines       (str/split-lines data)
        line-vecs-1 (mapv #(str/split % #"\|" ) lines)]
       (for [x line-vector-c] (print (line-vector-c 1))
       )
  )
)

gives me: 给我:

 [2 Sue Jones 43 Rose Court Street 345-7867][2 Sue Jones 43 Rose Court Street 
 345-7867][2 Sue Jones 43 Rose Court Street 345-7867]

what I want: 我想要的是:

"John Smith"
"Sue Jones"
"Fang Yuhong"

What you've got so far (slightly rewritten) is: 到目前为止(略作重写),您将得到:

(mapv (fn [l]
        (str/split l #"\|"))
      (str/split-lines data))

(str/split-lines data) splits the lines into a sequence of strings: (str/split-lines data)将这些行分成一系列字符串:

["1|John Smith|123 Here Street|456-4567"
 "2|Sue Jones|43 Rose Court Street|345-7867"
 "3|Fan Yuhong|165 Happy Lane|345-4533"]

(mapv #(str/split % #"\\|") lines) splits each line into tuples of strings: (mapv #(str/split % #"\\|") lines)将每一行拆分为字符串元组:

[["1" "John Smith" "123 Here Street" "456-4567"] 
 ["2" "Sue Jones" "43 Rose Court Street" "345-7867"]
 ["3" "Fan Yuhong" "165 Happy Lane" "345-4533"]]

Now you want to transform each tuple of strings into just the 2nd element of each tuple. 现在,您想将字符串的每个元组仅转换为每个元组的第二个元素。 There are a couple of functions you could use for that: get or nth (both are zero-based). 您可以使用以下两个函数: getnth (都基于零)。

For example: 例如:

(mapv (fn [l]
        (get (str/split l #"\|")
             1))
      (str/split-lines data))

You can get a list of names using 您可以使用获取名称列表

(def names
  (sequence
    (comp
      (map #(str/split % #"\|"))
      (map second))
    (str/split-lines data)))

Then print each name with 然后用

(doseq [n names]
  (println n))

I often use sequence and composed transducers to explore data, as it's convenient for building up transformations one step at a time. 我经常使用sequence和组合换能器来探索数据,因为它一次可以一步一步建立转换。

You can split each line and get the second column like this 您可以分割每一行并像这样获得第二列

(defn test [xs]
    (->> (str/split-lines xs)       ;split lines
         (map #(str/split % #"\|")) ;get columns
         (map second)))             ;only the second element of each line

and print the result 并打印结果

(map println (test data))

or better using doseq for printing only 或更好地使用剂量q仅用于打印

(doseq [n (test data)]
  (println n))

Using RegEx: 使用RegEx:

(map second (re-seq #"\|(.*?)\|" data))

returns: 收益:

("John Smith" "Sue Jones" "Fan Yuhong")

Explaining: 解释:

re-seq 再以次

We ask re-seq to find at each line the first content between a pair of | 我们要求re-seq在每一行中找到一对|之间的第一内容| . The ? ? in the regex means we want a non greedy search. 在正则表达式中表示我们希望进行非贪婪搜索。

(re-seq #"\|(.*?)\|" data)

returns: 收益:

(["|John Smith|" "John Smith"] ["|Sue Jones|" "Sue Jones"] ["|Fan Yuhong|" "Fan Yuhong"])

map 地图

Finally, we use map second to visit each element of the list and get only the second string of the vector. 最后,我们使用map second访问列表的每个元素,并仅获取向量的第二个字符串。

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

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