对于整数,可以使用列表推导式来完成:
julia> nums = [1,2,3,4]
4-element Array{Int64,1}:
1
2
3
4
julia> nums = [i+1 for i in nums]
4-element Array{Int64,1}:
2
3
4
5
但是当我尝试向一个字符串中添加一个字符时,它会抛出错误:
julia> s = split("the lazy fox jumps over the brown dog")
8-element Array{SubString{String},1}:
"the"
"lazy"
"fox"
"jumps"
"over"
"the"
"brown"
"dog"
julia> s2 = [word +'X' for word in s]
ERROR: syntax: invalid comprehension syntax
当字符是一个私有的Unicode字符(http://www.fileformat.info/info/unicode/char/e000/index.htm)时,会抛出另一个错误:
julia> s2 = [word + u'\uE000' for word in s]
错误:未定义变量错误:u未定义 在./array.jl:307中的collect(::Base.Generator{Array{SubString{String},1},##3#4})
如何在Julia中将Unicode字符/字符串附加到字符串列表?
*
而不是+
。另外,它仅适用于字符串,例如"x"
,而不是字符,例如'x'
。您的最后一个示例将读作s2 = [word * "\ue000" for word in s]
。 - timfor c in s2[1] println(c) end
。 - tims2[length(s2)][length(s2[length(s2)])]
->\ue000
- Nat Gillin