简体   繁体   English

多维数组,格式为julia script / print()中的shell Array输出

[英]A multidimensional array formatted with shell Array output in julia script/print()

When in the Julia shell if you run the function zeros(5, 5) you get something that looks like this: 在Julia外壳中,如果您运行功能zeros(5, 5)则会得到如下所示的内容:

 0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0

If you store the multidimensional array in a variable and print it (or directly print it) in the shell or an external script, you will get the much uglier: 如果将多维数组存储在变量中并在外壳程序或外部脚本中将其打印(或直接打印),则会变得更加难看:

[0.0 0.0 0.0 0.0 0.0; 0.0 0.0 0.0 0.0 0.0; 0.0 0.0 0.0 0.0 0.0; 0.0 0.0 0.0 0.0 0.0; 0.0 0.0 0.0 0.0 0.0]

Is there a way to access the Array's builtin STDOUT formatter that displays it the human readable way in the shell? 有没有一种方法可以访问数组的内置STDOUT格式化程序,从而在外壳中以人类可读的方式显示它?

Use display(x) instead of print(x) . 使用display(x)而不是print(x)

Note that print(x) can be useful in situations where you need copy-paste-runnable code. 请注意,在需要可复制粘贴运行的代码的情况下, print(x)很有用。

to complete @crstnbr answer I would also suggest show 完成@crstnbr答案,我也建议显示

M=rand(2,3)
f = open("test.txt","w")
show(f, "text/plain", M)
close(f)

then if you read and print test.txt you get: 然后,如果您阅读并打印test.txt,则会得到:

julia> print(read("test.txt",String))
2×3 Array{Float64,2}:
 0.73478   0.184505  0.0678265
 0.309209  0.204602  0.831286 

note: instead of file f you can also use stdout. 注意:除了文件f外,您还可以使用stdout。

To save some data in a stream the function show is more suited than display , as explained in the docs ( ?display ): 要在流中保存一些数据,函数showdisplay更合适,如docs( ?display )中所述:

  In general, you cannot assume that display output goes to stdout (unlike print(x) or show(x)). For example, display(x) may open up a separate window with an image. display(x) means "show x in the best way you can for the current output device(s)." If you want REPL-like text output that is guaranteed to go to stdout, use show(stdout, "text/plain", x) instead. 

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

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