问题
I have a vector of vectors, and I want to print elements in each vector
I tried the pprint but did not work as wanted
This is the vector of vectors I wish to print:
[["+" "+" "+" "#" "!" "-" "#" "#" "#" "-" "-" "-" "-"]
["!" "#" "+" "+" "+" "#" "+" "+" "+" "-" "#" "#" "-"]
["#" "#" "#" "#" "+" "#" "+" "#" "+" "#" "-" "#" "#"]
["+" "+" "+" "#" "+" "+" "+" "#" "+" "#" "-" "-" "-"]
["+" "#" "+" "#" "#" "#" "#" "+" "+" "-" "#" "#" "-"]
["+" "#" "+" "+" "+" "+" "+" "+" "#" "-" "-" "-" "-"]
["+" "#" "#" "#" "#" "#" "#" "#" "#" "#" "#" "#" "#"]
["+" "+" "+" "+" "+" "+" "+" "+" "+" "+" "+" "+" "@"]]
This is the output I want:
+++#--###--#-
!#+++#+++-##-
####+#+#+#!##
+++#+++#+#!!!
+#+####++!##!
+#++++++#!!!!
+############
++++++++++++@
回答1:
You can use cl-format to handle list (and list of list in this case) with a control string:
(def input
[["+" "+" "+" "#" "!" "-" "#" "#" "#" "-" "-" "-" "-"]
["!" "#" "+" "+" "+" "#" "+" "+" "+" "-" "#" "#" "-"]
["#" "#" "#" "#" "+" "#" "+" "#" "+" "#" "-" "#" "#"]
["+" "+" "+" "#" "+" "+" "+" "#" "+" "#" "-" "-" "-"]
["+" "#" "+" "#" "#" "#" "#" "+" "+" "-" "#" "#" "-"]
["+" "#" "+" "+" "+" "+" "+" "+" "#" "-" "-" "-" "-"]
["+" "#" "#" "#" "#" "#" "#" "#" "#" "#" "#" "#" "#"]
["+" "+" "+" "+" "+" "+" "+" "+" "+" "+" "+" "+" "@"]])
(use 'clojure.pprint)
(cl-format *out* "~{~{~a~}~%~}" input)
Output:
+++#!-###----
!#+++#+++-##-
####+#+#+#-##
+++#+++#+#---
+#+####++-##-
+#++++++#----
+############
++++++++++++@
Explanation:
~a is the format argument of any type. When enclosed in ~{ and ~}, the format argument will be iterated over a list. So when enclosed twice with ~{ and ~}, ~a will be applied over each element (character) in the nested list. The ~% before the last ~} is to output a newline.
With this, you can easily change the output with control string. For example:
(cl-format *out* "~{~{(~a)~^ ~}~^~%~%~}" input)
(+) (+) (+) (#) (!) (-) (#) (#) (#) (-) (-) (-) (-)
(!) (#) (+) (+) (+) (#) (+) (+) (+) (-) (#) (#) (-)
(#) (#) (#) (#) (+) (#) (+) (#) (+) (#) (-) (#) (#)
(+) (+) (+) (#) (+) (+) (+) (#) (+) (#) (-) (-) (-)
(+) (#) (+) (#) (#) (#) (#) (+) (+) (-) (#) (#) (-)
(+) (#) (+) (+) (+) (+) (+) (+) (#) (-) (-) (-) (-)
(+) (#) (#) (#) (#) (#) (#) (#) (#) (#) (#) (#) (#)
(+) (+) (+) (+) (+) (+) (+) (+) (+) (+) (+) (+) (@)
Note that "~^" is the directive to stop iteration when a list is run out. So "~^<space>" will suppress the space after the last element in each line. And "~^~%~%" will suppress the double newline after the last line.
回答2:
You don't need any fancy formatting or side-effects (although I do appreciate the information in those answers). Just use clojure.string/join to create the string you want and print it any old way.
> (join "\n" (map join input))
+++#!-###----
!#+++#+++-##-
####+#+#+#-##
+++#+++#+#---
+#+####++-##-
+#++++++#----
+############
++++++++++++@
e.g.
> (let [s (join "\n" (map join input))] (println s))
+++#!-###----
!#+++#+++-##-
####+#+#+#-##
+++#+++#+#---
+#+####++-##-
+#++++++#----
+############
++++++++++++@
nil
回答3:
We can use run! to go over the outer vector and clojure.string/join to turn each inner vector into a printable string.
Assuming your vector is defined as input:
(require '[clojure.string :as str])
(run! (comp println str/join) input)
来源:https://stackoverflow.com/questions/56369017/how-to-print-elements-in-a-vector-of-vectors