must be obvious, but I have tried many
julia> d = Dict("One" => 1, "Two" => 2, "Three" => 3); String(keys(d))
ERROR: MethodError: Cannot `convert` an object of type Base.KeyIterator{Dict{String,Int64}} to an object of type String
This may have arisen from a call to the constructor String(...),
since type constructors fall back to convert methods.
I have tried many variations, using convert, etc.
Maybe you want collect(keys(d))
?
thanks, steward, but not enough. I would like to understand how to convert this from substrings to strings. the following is an example, not to get a solution, but to illustrate the difference. I want to just get a simple one-dimensional string array out of split.
julia> mystring="a ab ac ab ab ad ba"
"a ab ac ab ab ad ba"
julia> wordcounts= Dict{String,Int64}; \
for word in collect(split(mystring)); wordcounts[word]= get(wordcounts, word, 0) + 1; end
ERROR: MethodError: no method matching get(::Type{Dict{String,Int64}}, ::SubString{String}, ::Int64)
Closest candidates are:
get(::ObjectIdDict, ::ANY, ::ANY) at associative.jl:434
get(::Base.EnvHash, ::AbstractString, ::Any) at env.jl:79
get(::Dict{K,V}, ::Any, ::Any) where {K, V} at dict.jl:478
...
Stacktrace:
[1] macro expansion at ./REPL[10]:1 [inlined]
[2] anonymous at ./<missing>:?
It works, just do wordcounts = Dict{String,Int}()
to actually create a dict object rather than referring to the type itself. You don’t even need to call collect
.
In general it’s a good idea to provide a reproducible example, as it makes it clear what you want to do, and what might be the problem.
thanks. this was very helpful for my specific example, but let’s say I am not only stupid but also stubborn, and I really want to know how to convert the type of split(mystring) from an Array{Substring{String},1})
into an Array(String,1)
. How would I do this?