Dict from 2d arrays and 1d arrays as keys

I am trying to create a (touple) → value dictionary out of a 2d arrays table of values and a 1d array of keys.

This code (not mine) works…

using DataArrays, DataFrames

# This works....

orig = ["Epinal", "Bordeaux", "Grenoble"]
prod = ["Fuelwood", "Sawnwood", "Pannels"]
supplytable = wsv"""
prod     Epinal Bordeaux Grenoble
Fuelwood 400    700      800
Sawnwood 800    1600     1800
Pannels  200    300      300
supply = Dict( (r[:prod],o) => r[Symbol(o)] for r in eachrow(supplytable), o in orig)

This however doesn’t work (“LoadError: ArgumentError: Dict(kv): kv needs to be an iterator of tuples or pairs”):

# This doesn't work...
s   = ["Chêne pédonculé", "Chêne sessile", "Hêtre", "Pin sylvestre"]
σtable = [[0.037502535947712	0.016082745098039	0.027797176470588	-0.025589882352942]
          [0.016082745098039	0.015177019607843	0.018791960784314	-0.102880470588234]
          [0.027797176470588	0.018791960784314	0.031732078431373	-0.166391058823529]
          [-0.025589882352942	-0.102880470588234	-0.166391058823529	2.02950454411765]]
σ = Dict( (i,j) => σtable[i,j] for i in s, j in s)

What’s the difference ??
I’m sure I can find alternatives, but I am curious why it doesn’t work, as I tried to mimic the above code that works…

You are trying to index σtable with i and j with strings, which doesn’t make sense since it’s an array. Try with σ = Dict( (s[i], s[j]) => σtable[i,j] for i in 1:length(s), j in 1:length(s) ).

By the way, for this kind of data structure, you might be interested in the NamedArrays package instead of using a dict.


sure, thank you…

Also working:
σ = Dict((i,j) => σtable[i_ix,j_ix] for (i_ix,i) in enumerate(s), (j_ix,j) in enumerate(s))