gpt4 book ai didi

string - 如何将 unicode 字符串附加到 Julia 中的字符串列表?

转载 作者:行者123 更新时间:2023-12-04 02:06:24 26 4
gpt4 key购买 nike

对于整数,可以通过以下方式完成列表理解:

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

当字符是私有(private) unicode 字符 ( http://www.fileformat.info/info/unicode/char/e000/index.htm ) 时,它会抛出另一个错误:

julia> s2 = [word + u'\uE000' for word in s]

错误:UndefVarError:你没有定义 在 ./array.jl:307 处收集(::Base.Generator{Array{SubString{String},1},##3#4})

如何将 unicode 字符/字符串附加到 Julia 中的字符串列表?

最佳答案

要快速回答你的问题,正确的做法是

[i*"X" for i in s]

但是,请记住 'X' 是和 "X" 是两个不同的东西。

julia> val = 'X'
'X'
julia> typeof(val)
Char

julia> val = "X"
"X"
julia> typeof(val)
String

如果您有 Python 背景,这可能最初会发生,而两者之间没有区别。

Julia 中的字符串连接也是由 * 而不是 + 完成的!你可能会问为什么会这样!那么看看这个帖子here

关于string - 如何将 unicode 字符串附加到 Julia 中的字符串列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43112806/

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