Naa*_*aar 2 arrays string julia
我想要一个包含一系列数字但将它们存储为字符串的数组。这是我需要的示例输出:
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
Run Code Online (Sandbox Code Playgroud)
我尝试了这个,但它生成一个以数组作为其值的字符串。
julia> string([0:9...])
"[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"
Run Code Online (Sandbox Code Playgroud)
那么我怎样才能生成这样的东西呢?
为了更好地理解,这就是在 python 中完成的方法:
>>> list('0123456789')
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
Run Code Online (Sandbox Code Playgroud)
首先,请注意 Julia 对于Char
(单个字符)和String
类型有单独的类型。在 Julia 中,['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
将是一个字符数组,可以创建为:
julia> '0':'9'
'0':1:'9'
Run Code Online (Sandbox Code Playgroud)
您可以通过执行以下操作来验证它是否包含上述数组:
julia> '0':'9' |> collect |> print
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
Run Code Online (Sandbox Code Playgroud)
但如果你确实希望结果是String
s 而不是Char
s,你可以使用广播来生成它:
julia> v = 1001:1010 # your input range here
1001:1010
julia> string.(v)
10-element Vector{String}:
"1001"
"1002"
"1003"
"1004"
"1005"
"1006"
"1007"
"1008"
"1009"
"1010"
Run Code Online (Sandbox Code Playgroud)