您可以直接使用它們直接指向字典的鍵,例如,
points = [(rand(),rand()) for i in 1:100]
a_dict = [p => rand(1:10000) for p in points]
@show points[1]
@show a_dict[points[1]]
產生
points[1] => (0.4374267407775083,0.32232663537074036)
a_dict[points[1]] => 4102
編輯:這個問題是問列表點,而不是單對。它很難從你的問題告訴我們,但如果你想拋出點的名單,然後我只是用hash
功能,這是一本字典的用途:
julia> x = [(rand(),rand()) for i in 1:100];
julia> y = copy(x);
julia> z = [(rand(),rand()) for i in 1:100];
julia> hash(x)
0x2dd258e3af0ec93a
julia> hash(y)
0x2dd258e3af0ec93a
julia> hash(z)
0x6449b750e42e6bc6
julia> some_dict = [hash(x) => 1, hash(z) => 1]
Dict{Uint64,Int64} with 2 entries:
0x6449b750e42e6bc6 => 1
0x2dd258e3af0ec93a => 1
julia> if hash(y) in keys(some_dict)
some_dict[hash(y)] += 1
else
some_dict[hash(y)] = 1
end
julia> some_dict
Dict{Uint64,Int64} with 2 entries:
0x6449b750e42e6bc6 => 1
0x2dd258e3af0ec93a => 2
如果你確實需要檢索什麼點集,雖然,這似乎是有可能的,那麼你可以使用字典直接與設定點爲關鍵,即
julia> other_dict = [x=>1,z=>1]
Dict{Array{(Float64,Float64),1},Int64} with 2 entries:
[(0.6453074757765922,0.0958215… => 1
[(0.5283378708792779,0.8418390… => 1
julia> other_dict[y] += 1
2
julia> other_dict
Dict{Array{(Float64,Float64),1},Int64} with 2 entries:
[(0.6453074757765922,0.0958215… => 1
[(0.5283378708792779,0.8418390… => 2
這(使用可變密鑰)是朱莉婭完全正常的(雖然當然,如果你更改了字典的鍵,當然會發生不好的事情)。
爲什麼不直接使用x,y對作爲鍵? – rickhg12hs 2014-10-20 10:18:41
我編輯了原始評論,使其更清晰一些。我期待在字典中使用一組唯一有序對作爲關鍵字。 – koozdra 2014-10-20 17:54:18