我正在嘗試使用Integer
和Vector{Integer}
子集值的組合在Julia中對Vector{String}
進行子集化。我想編寫一個函數,基本上允許的"asdf"[1:3]
與三個參數x[y:z]
的子集是任何載體或單身。在Julia中使用向量來處理字符串向量中的元素
這是我迄今爲止嘗試:
function substring(x::Array{String}, y::Integer, z::Integer)
y = fill(y, length(x))
z = fill(z, length(x))
substring(x, y, z)
end
function substring(x::Vector{String}, y::Vector{Integer}, z::Integer)
y = fill(y, length(x))
substring(x, y, z)
end
function substring(x::Vector{String}, y::Integer, z::Vector{Integer})
z = fill(z, length(x))
substring(x, y, z)
end
function substring(x::Vector{String}, y::Vector{Integer}, z::Vector{Integer})
for i = 1:length(x)
x[i] = x[i][y[i]:min(z[i], length(x[i]))]
# If z[i] is greater than the length of x[i]
# return the end of the string
end
x
end
嘗試使用它:
v = string.('a':'z')
x = rand(v, 100) .* rand(v, 100) .* rand(v, 100)
substring(x, 1, 2)
# or
substring(x, 1, s)
我得到的錯誤:
MethodError: no method matching substring(::Array{String,1}, ::Int64, ::Array{Int64,1})
Closest candidates are:
substring(::Array{String,N}, ::Integer, !Matched::Integer) at untitled-e3b9271a972031e628a35deeeb23c4a8:2
substring(::Array{String,1}, ::Integer, !Matched::Array{Integer,1}) at untitled-e3b9271a972031e628a35deeeb23c4a8:13
substring(::Array{String,N}, ::Integer, !Matched::Array{Integer,N}) at untitled-e3b9271a972031e628a35deeeb23c4a8:13
...
in include_string(::String, ::String, ::Int64) at eval.jl:28
in include_string(::Module, ::String, ::String, ::Int64, ::Vararg{Int64,N}) at eval.jl:32
in (::Atom.##53#56{String,Int64,String})() at eval.jl:50
in withpath(::Atom.##53#56{String,Int64,String}, ::Void) at utils.jl:30
in withpath(::Function, ::String) at eval.jl:38
in macro expansion at eval.jl:49 [inlined]
in (::Atom.##52#55{Dict{String,Any}})() at task.jl:60
我看到有另一個post addressing與Vector{String}
類似的錯誤。我的帖子還回應了與Vector{Integer}
相關的錯誤。我相信對它的迴應可能有助於像我這樣的人發現抽象類型的實現新穎和困難。
的可能的複製[向量{AbstractString}函數參數將不接受在朱向量{字符串}輸入](http://stackoverflow.com/questions/21465838/vectorabstractstring-function-parameter-wont-accept-vectorstring- input-in-j) –
這是一個參數不變性的例子。見http://stackoverflow.com/questions/21465838/vectorabstractstring-function-parameter-wont-accept-vectorstring-input-in-j對於s類似的問題;這裏你的問題在'Vector {Integer}'中。 –
我想指出,雖然我的問題已被解釋爲類型管理問題。我真的只是在尋找一個符合標題所說的功能。在我更習慣的R中,答案是微不足道的,'substr(x,1,2)'。我列出了上面的代碼,以表明我已經做出了合理的努力來自己解決這個問題。如果沒有太多的麻煩,我會非常感謝答案。 – fsmart