本文介绍了Julia:大型数组的有限打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Julia 中使用脚本文件生成了许多大型数组.打印出整个数组很麻烦,但我想检查前几行是否有意义.

I produce a number of large arrays in Julia using a script file. Printing out the whole array is cumbersome but I'd like to check the first few rows make sense.

我知道在 REPL 中有打印受限于屏幕尺寸,例如

I know in the REPL there's printing which is limited by the screen size e.g.

julia> zeros(1000,10)
1000×10 Array{Float64,2}:
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
0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0

但我在基本 Julia 中找不到任何打印/显示函数来模拟脚本,比如只打印出数组的前 10 行或类似 R 的 head 的东西(我会期望 showcompact 做这样的事情).

But I can't find any print/show function in base Julia which mimics this for scripts, say only printing out the first 10 rows of an array or something like R's head (I would have expected showcompact to do something like this).

Julia 中是否有与 R 的 head 类似的功能,还是我必须自己编写.

Is there an analogous function to R's head in Julia or do I have to write my own.

推荐答案

正如我在评论中提到的,在 v0.5 中执行此操作的方法是使用 IOContext.

As I mentioned in a comment, the way to do this in v0.5 is to use an IOContext.

限制数据的一个非常简单的方法是通过 :limit =>;IOContext 的 true 参数;

A very simple way to limit the data is to pass the :limit => true parameter to the IOContext;

julia> show(IOContext(stdout, :limit => true), v)
[0.147959 0.414018 … 0.595528 0.852563; 0.32679 0.824953 … 0.432143 0.036279; … ; 0.877398 0.661854 … 0.197207 0.15596; 0.0522946 0.508075 … 0.835359 0.705987]

但这仍然不能像 REPL 那样打印出来;这是因为带有两个参数的 show 使用单行显示.要使用多行显示,请将 text/plain 作为第二个参数传递给 show(一种 MIME 类型):

But this still does not print out the way the REPL does; that's because show with two arguments uses a single-line display. To use a multiline display, pass text/plain as the second argument to show (a MIME type):

julia> show(IOContext(stdout, :limit => true), "text/plain", v)
100×100 Array{Float64,2}:
 0.147959   0.414018   0.0282934  …  0.816132   0.595528   0.852563
 0.32679    0.824953   0.0582351     0.822526   0.432143   0.036279
 0.754989   0.724317   0.533966      0.987273   0.931932   0.973622
 0.547866   0.282694   0.0295411     0.75929    0.886218   0.0813057
 0.0626663  0.111795   0.625083      0.439983   0.562143   0.669046
 0.712093   0.469622   0.377298   …  0.298224   0.31853    0.376066
 0.774625   0.754328   0.756725      0.61113    0.76566    0.999292
 0.917846   0.308363   0.489246      0.715311   0.175302   0.124059
 0.310922   0.140575   0.20635       0.0280192  0.683004   0.168129
 0.753361   0.755103   0.831806      0.118009   0.122374   0.281476
 ⋮                                ⋱
 0.420264   0.7614     0.748408      0.330983   0.0776789  0.309464
 0.984379   0.851735   0.595121      0.534982   0.255317   0.743713
 0.814505   0.765941   0.71852       0.730677   0.477631   0.0360992
 0.910384   0.0747604  0.490685      0.0904559  0.0756424  0.313898
 0.628416   0.0790874  0.401488   …  0.523521   0.397249   0.58112
 0.578361   0.336352   0.261118      0.838256   0.387374   0.451647
 0.66724    0.586342   0.378968      0.602694   0.450686   0.901279
 0.877398   0.661854   0.685156      0.658952   0.197207   0.15596
 0.0522946  0.508075   0.244423      0.95935    0.835359   0.705987

您当然可以通过将 :displaysize 传递给 IOContext 来更改显示的行数:

You can of course change how many rows are shown by passing in :displaysize to the IOContext:

julia> show(IOContext(stdout, :limit => true, :displaysize => (10, 10)), "text/plain", v)

100×100 Array{Float64,2}:
 0.147959   …  0.852563
 0.32679       0.036279
 0.754989      0.973622
 ⋮          ⋱
 0.877398      0.15596
 0.0522946     0.705987

总的来说,IOContext 非常灵活.有关详细信息,请参阅其文档.

Overall, IOContext is very flexible. See its documentation for more details.

这篇关于Julia:大型数组的有限打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 02:22
查看更多