gpt4 book ai didi

printing - 将向量折叠为 julia 中的逗号分隔字符串

转载 作者:行者123 更新时间:2023-12-01 11:21:55 25 4
gpt4 key购买 nike

我有一维值向量。我想将它们转换为一个字符串,其中的值以逗号分隔。 Julia 有没有一种简单的方法来做到这一点?类似崩溃的东西 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

最佳答案

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

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

您还可以使用逗号连接迭代中的每个项目:
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

关于printing - 将向量折叠为 julia 中的逗号分隔字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41294485/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com