如何在向量的向量中打印元素 [英] How to print elements in a vector of vectors
问题描述
我有一个向量矢量,我想在每个向量中打印元素
I have a vector of vectors, and I want to print elements in each vector
我尝试了pprint但没有按要求工作
I tried the pprint but did not work as wanted
这是我要打印的向量的向量:
This is the vector of vectors I wish to print:
[["+" "+" "+" "#" "!" "-" "#" "#" "#" "-" "-" "-" "-"]
["!" "#" "+" "+" "+" "#" "+" "+" "+" "-" "#" "#" "-"]
["#" "#" "#" "#" "+" "#" "+" "#" "+" "#" "-" "#" "#"]
["+" "+" "+" "#" "+" "+" "+" "#" "+" "#" "-" "-" "-"]
["+" "#" "+" "#" "#" "#" "#" "+" "+" "-" "#" "#" "-"]
["+" "#" "+" "+" "+" "+" "+" "+" "#" "-" "-" "-" "-"]
["+" "#" "#" "#" "#" "#" "#" "#" "#" "#" "#" "#" "#"]
["+" "+" "+" "+" "+" "+" "+" "+" "+" "+" "+" "+" "@"]]
这是我想要的输出:
+++#--###--#-
!#+++#+++-##-
####+#+#+#!##
+++#+++#+#!!!
+#+####++!##!
+#++++++#!!!!
+############
++++++++++++@
推荐答案
您可以使用 cl格式使用控制字符串处理列表(在这种情况下为列表的列表):
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)
输出:
+++#!-###----
!#+++#+++-##-
####+#+#+#-##
+++#+++#+#---
+#+####++-##-
+#++++++#----
+############
++++++++++++@
说明:
〜a
是任何类型的format参数。当包含在〜{
和〜}
中时,format参数将在列表上迭代。因此,当用〜{
和〜}
包围两次时,〜a
将应用于嵌套列表中的每个元素(字符)。最后一个〜}
之前的〜%
是输出换行符。
这样,您可以轻松地使用控制字符串更改输出。例如:
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)
(+) (+) (+) (#) (!) (-) (#) (#) (#) (-) (-) (-) (-)
(!) (#) (+) (+) (+) (#) (+) (+) (+) (-) (#) (#) (-)
(#) (#) (#) (#) (+) (#) (+) (#) (+) (#) (-) (#) (#)
(+) (+) (+) (#) (+) (+) (+) (#) (+) (#) (-) (-) (-)
(+) (#) (+) (#) (#) (#) (#) (+) (+) (-) (#) (#) (-)
(+) (#) (+) (+) (+) (+) (+) (+) (#) (-) (-) (-) (-)
(+) (#) (#) (#) (#) (#) (#) (#) (#) (#) (#) (#) (#)
(+) (+) (+) (+) (+) (+) (+) (+) (+) (+) (+) (+) (@)
请注意, 〜^
是在运行列表时停止迭代的指令出来。因此, 〜^< space>
将取消每行最后一个元素之后的空格。 〜^〜%〜%
将取消最后一行之后的双换行符。
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.
这篇关于如何在向量的向量中打印元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!