awesome thanks so much this is really helpful! I’m not sure if its because I’m using version 1.8.3 but when I enter in
dict = [x => Vector{Float64}(undef, 10) for x in entries]
I don’t get a dictionary but a vector
julia> dict = [x => Vector{Float64}(undef, 10) for x in entries]
3-element Vector{Pair{String, Vector{Float64}}}:
"a" => [5.3696034695e-314, 0.0, 1.0e-323, 5.3696034695e-314, 1.5e-323, 1.0e-323, 5.3696034695e-314, 3.0e-323, 1.0e-323, 5.3696034695e-314]
"b" => [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 6.52171459e-315, 5.3696034695e-314, 0.0]
"c" => [4.4e-323, 3.0e-323, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
julia> typeof(dict)
Vector{Pair{String, Vector{Float64}}} (alias for Array{Pair{String, Array{Float64, 1}}, 1})
Then when I try to access the entries I get an error
julia> dict["a"]
ERROR: ArgumentError: invalid index: "a" of type String
Stacktrace:
[1] to_index(i::String)
@ Base ./indices.jl:300
[2] to_index(A::Vector{Pair{String, Vector{Float64}}}, i::String)
@ Base ./indices.jl:277
[3] to_indices
@ ./indices.jl:333 [inlined]
[4] to_indices
@ ./indices.jl:325 [inlined]
[5] getindex(A::Vector{Pair{String, Vector{Float64}}}, I::String)
@ Base ./abstractarray.jl:1241
[6] top-level scope
@ REPL[161]:1
But if I try
dict2 =Dict([x => Vector{Float64}(undef, 10) for x in entries])
it seems to work. Also is there a way to do this without using a dictionary? My ultimate goal is to initiate these vectors in a function so that I can populate them and then add them to a DataFrame like this via bkamins post here How to initialize empty dataframe of specified size
julia> function test2()
nt = (a=Vector{Int}(undef, 10^6),
b=Vector{String}(undef, 10^6),
c=Vector{Int}(undef, 10^6))
for i in 1:10^6
nt.a[i] = 1
nt.b[i] = "1"
nt.c[i] = 1.0
end
return DataFrame(nt, copycols=false)
end
test2 (generic function with 1 method)
Will using a dictionary as opposed to just vectors affect performance?