Julia 中的排序字典
Sorting Dictionary in Julia
我在 Julia 中有一本字典,我想按值排序。我找到了几种方法来做到这一点。例如
dict = Dict(i => sqrt(i*rand()) for i = 1:20)
dict= sort(dict;byvalue = true) # method 1
dict = OrderedDict(i => sqrt(i*rand()) for i = 1:20) #method 2 using ordereddict package [https://github.com/JuliaCollections/DataStructures.j][1]
# I don't want to use collect() method as I do not want the tuples of dictionary.
但是,有时这两种方法在从一个函数传递到另一个函数时确实会丢失它们的顺序。在 Julia 中是否有任何其他方法可以使有序字典不可变?
根据 docs,对于 OrderedDict
order refers to insertion order.
我不知道这怎么会“失去它的顺序”,但也许你只是在改变东西?
可能你想要的更接近SortedDict
;但是,这是按键而不是值排序的。按值排序的字典有点不寻常。
如果你想要一个具有快速按键查找和迭代按值排序的可变数据结构,你可以通过两级方法来模拟它:一个普通的字典用于存储原始键和标记之间的映射,第二个SortedMultiDict{ValueType, Nothing}
模拟令牌索引到的排序多重集。然后你定义你自己的机制来通过令牌以某种方式进行间接查找,如下所示:
function insert!(d::ValueSortedDict, k, v)
_, token = insert!(d.values, v, nothing)
d.keys[k] = token
end
getindex(d::ValueSortedDict, k) = deref_key((d.values, d.keys[k]))
其他 get/set 样式函数也相应。 (我没有测试这个,它只是阅读了文档。)
OTOH,如果你从不打算改变事物,你可以做一个非常相似的事情,你将 Dict{KeyType, Int}
和 Vector{ValueType}
存储在一起,并且 sort!
向量一次在开始。 (Dictionaries.jl,在@mcabbot 的回答中描述,基本上就是在实现这个。)
您可能对Dictionaries.jl感兴趣:
julia> dict = Dict(i => (i/10 + rand(1:99)) for i = 1:7)
Dict{Int64, Float64} with 7 entries:
5 => 16.5
4 => 23.4
6 => 98.6
7 => 56.7
2 => 7.2
3 => 58.3
1 => 85.1
julia> using Dictionaries
julia> sort(Dictionary(dict))
7-element Dictionary{Int64, Float64}
2 │ 7.2
5 │ 16.5
4 │ 23.4
7 │ 56.7
3 │ 58.3
1 │ 85.1
6 │ 98.6
julia> map(sqrt, ans)
7-element Dictionary{Int64, Float64}
2 │ 2.6832815729997477
5 │ 4.06201920231798
4 │ 4.8373546489791295
7 │ 7.52994023880668
3 │ 7.635443667528429
1 │ 9.22496612459905
6 │ 9.929753269845127
我在 Julia 中有一本字典,我想按值排序。我找到了几种方法来做到这一点。例如
dict = Dict(i => sqrt(i*rand()) for i = 1:20)
dict= sort(dict;byvalue = true) # method 1
dict = OrderedDict(i => sqrt(i*rand()) for i = 1:20) #method 2 using ordereddict package [https://github.com/JuliaCollections/DataStructures.j][1]
# I don't want to use collect() method as I do not want the tuples of dictionary.
但是,有时这两种方法在从一个函数传递到另一个函数时确实会丢失它们的顺序。在 Julia 中是否有任何其他方法可以使有序字典不可变?
根据 docs,对于 OrderedDict
order refers to insertion order.
我不知道这怎么会“失去它的顺序”,但也许你只是在改变东西?
可能你想要的更接近SortedDict
;但是,这是按键而不是值排序的。按值排序的字典有点不寻常。
如果你想要一个具有快速按键查找和迭代按值排序的可变数据结构,你可以通过两级方法来模拟它:一个普通的字典用于存储原始键和标记之间的映射,第二个SortedMultiDict{ValueType, Nothing}
模拟令牌索引到的排序多重集。然后你定义你自己的机制来通过令牌以某种方式进行间接查找,如下所示:
function insert!(d::ValueSortedDict, k, v)
_, token = insert!(d.values, v, nothing)
d.keys[k] = token
end
getindex(d::ValueSortedDict, k) = deref_key((d.values, d.keys[k]))
其他 get/set 样式函数也相应。 (我没有测试这个,它只是阅读了文档。)
OTOH,如果你从不打算改变事物,你可以做一个非常相似的事情,你将 Dict{KeyType, Int}
和 Vector{ValueType}
存储在一起,并且 sort!
向量一次在开始。 (Dictionaries.jl,在@mcabbot 的回答中描述,基本上就是在实现这个。)
您可能对Dictionaries.jl感兴趣:
julia> dict = Dict(i => (i/10 + rand(1:99)) for i = 1:7)
Dict{Int64, Float64} with 7 entries:
5 => 16.5
4 => 23.4
6 => 98.6
7 => 56.7
2 => 7.2
3 => 58.3
1 => 85.1
julia> using Dictionaries
julia> sort(Dictionary(dict))
7-element Dictionary{Int64, Float64}
2 │ 7.2
5 │ 16.5
4 │ 23.4
7 │ 56.7
3 │ 58.3
1 │ 85.1
6 │ 98.6
julia> map(sqrt, ans)
7-element Dictionary{Int64, Float64}
2 │ 2.6832815729997477
5 │ 4.06201920231798
4 │ 4.8373546489791295
7 │ 7.52994023880668
3 │ 7.635443667528429
1 │ 9.22496612459905
6 │ 9.929753269845127