[英]A multidimensional array formatted with shell Array output in julia script/print()
在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
如果將多維數組存儲在變量中並在外殼程序或外部腳本中將其打印(或直接打印),則會變得更加難看:
[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]
有沒有一種方法可以訪問數組的內置STDOUT格式化程序,從而在外殼中以人類可讀的方式顯示它?
使用display(x)
而不是print(x)
。
請注意,在需要可復制粘貼運行的代碼的情況下, print(x)
很有用。
完成@crstnbr答案,我也建議顯示
M=rand(2,3)
f = open("test.txt","w")
show(f, "text/plain", M)
close(f)
然后,如果您閱讀並打印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
注意:除了文件f外,您還可以使用stdout。
要在流中保存一些數據,函數show比display更合適,如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.