在julia

时间:2016-12-23 02:38:45

标签: printing julia

我有1-d值向量。我想将它们转换为一个字符串,其值以逗号分隔。朱莉娅有一个简单的方法吗?类似r

中的崩溃
{julia}
julia> x = [24,122,63,24,83,56,54,175,11,11,24,51,85,92,74,500,80,127,126,59,111,155,132,202,64,164,1301]
#I want output like this as a string
#24,122,63,24,83,56,54,175,11,11,24,51,85,92,74,500,80,127,126,59,111,155,132,202,64,164,1301,27
#I have tried something like this
[julia> [print(i,",") for i in x]
24,122,63,24,83,56,54,175,11,11,24,51,85,92,74,500,80,127,126,59,111,155,132,202,64,164,1301,27-element Array{Void,1}:
 nothing
 nothing
 nothing
 nothing
 nothing
 nothing
 nothing
 nothing
 nothing
 nothing
 nothing
 nothing
 nothing
 nothing
 nothing
 nothing
 nothing
 nothing
 nothing
 nothing
 nothing
 nothing
 nothing
 nothing
 nothing
 nothing
 nothing

2 个答案:

答案 0 :(得分:4)

使用普通循环打印大部分值,然后打印最后一项(以消除尾随逗号):

julia> for i in @view x[1:end-1]
           print(i, ',')
       end; print(x[end])
24,122,63,24,83,56,54,175,11,11,24,51,85,92,74,500,80,127,126,59,111,155,132,202,64,164,1301

您还可以使用逗号加入iterable中的每个项目:

julia> print(join(x, ','))
24,122,63,24,83,56,54,175,11,11,24,51,85,92,74,500,80,127,126,59,111,155,132,202,64,164,1301

答案 1 :(得分:0)

此外,这更简单:print(string(x)[2:(end - 1)])