Hello, good morning.
How to add values in a dictionary that has type (int, vector(int))? Cant understand the error about conversion
ERROR: LoadError: MethodError: Cannot `convert` an object of type Int64 to an object of type Vector{Tuple{Int64}}
using StatsBase
using Plots
using Combinatorics
using DataStructures
Ω = Dict((i, j) => i+j for i in 1:6 for j in 1:6);
println(length(Ω))
println(Ω)
buffer = DefaultDict{Int64, Vector{Tuple{Int64}}}(0);
for ((i,j), value) in Ω
println("Key: i: $i, j: $j -> $value")
push!(buffer[value], (i, j));
end
What I want is create a key and append in vector of this key, because this will work as a histogram, and I will search for all the values of the vector using only the key
dict[7] will give-me all tuples (i,J) that i+j sums 7
But your code has still an issue.
You will never have more than one tuple in any array, because you overwrite if a key already exist in your Dict. You have to check if a key already exist.
Isn’t the point of using a DefaultDict that you don’t have to check if a key already exists?
I imagine this is how the original code was supposed to look, i.e. getting both the value type and the default constructor right.
using DataStructures
Ω = Dict((i, j) => i+j for i in 1:6 for j in 1:6);
println(length(Ω))
println(Ω)
buffer = DefaultDict{Int, Vector{Tuple{Int, Int}}}(Vector{Tuple{Int, Int}});
for ((i,j), value) in Ω
println("Key: i: $i, j: $j -> $value")
push!(buffer[value], (i, j));
end