如何访问 Dict 和 SortedDict 的 forward/reverse 顺序的键值元组? - 朱莉娅
How to access key-value tuples in forward/reverse order for Dict and SortedDict? - Julia
给定一个字典:
> d = Dict{Int, Int}(1=>123, 2=>51, 4=>23)
Dict{Int64,Int64} with 3 entries:
4 => 23
2 => 51
1 => 123
我可以通过键访问字典的值,例如:
> d[4]
23
或者我可以这样遍历键值对:
> for i in d
println(i)
end
4=>23
2=>51
1=>123
我试过将键作为列表的第一个元素访问,甚至 i.key
,但它似乎不是正确的语法:
julia> for i in d
println(i.key)
end
ERROR: type Pair has no field key
in macro expansion; at ./REPL[22]:2 [inlined]
in anonymous at ./<missing>:?
julia> for i in d
println(i[0])
end
ERROR: BoundsError: attempt to access 4=>23
at index [0]
in getindex(::Pair{Int64,Int64}, ::Int64) at ./operators.jl:609
in macro expansion; at ./REPL[23]:2 [inlined]
in anonymous at ./<missing>:?
然后我想起来 Julia 不是第 0 个索引,所以它应该是:
> for i in d
println(i[1], ' ', i[2])
end
4 23
2 51
1 123
> for i in d
println(i[1], ' ', i[2])
end
4 23
2 51
1 123
在这种情况下,当找不到列表的索引时,是不是有点像 Python 的 IndexError
?
问题的另一部分是关于SortedDict
,如何访问SortedDict
中的最后第N个元素?
我试过使用索引语法,我检索到了值,但没有检索到 (key,value)
的元组。
julia> import DataStructures: SortedDict
julia> sd = SortedDict(d)
DataStructures.SortedDict{Int64,Int64,Base.Order.ForwardOrdering} with 3 entries:
1 => 123
2 => 51
4 => 23
julia> sd[end]
23
另外,如何根据值对字典进行排序?
最后,如何反转排序后的字典?
我试过使用 Base.Order.ReverseOrding
但它抛出了一个 MethodError
:
julia> sd = SortedDict{Base.Order.ReverseOrdering}(d)
ERROR: MethodError: Cannot `convert` an object of type Dict{Int64,Int64} to an object of type DataStructures.SortedDict{Base.Order.ReverseOrdering,D,Ord<:Base.Order.Ordering}
This may have arisen from a call to the constructor DataStructures.SortedDict{Base.Order.ReverseOrdering,D,Ord<:Base.Order.Ordering}(...),
since type constructors fall back to convert methods.
in DataStructures.SortedDict{Base.Order.ReverseOrdering,D,Ord<:Base.Order.Ordering}(::Dict{Int64,Int64}) at ./sysimg.jl:53
我不是 Python 用户,而是 IndexError looks similar to the documentation for BoundsError 的文档。请注意,您始终可以使用 ?
查询当前的 Julia 文档,例如
?BoundsError
您可以对字典键和值进行排序:
d = Dict{Int,Int}(1 => 2, 3 => 4)
k = sort(collect(keys(d)) ### collect() forms an array that you can sort
v = sort(collect(values(d))
但我不明白您为什么要按值排序。为什么不简单地使用值作为键?
您可以轻松地遍历键或值:
for k in keys(d) ### or "for v in values(d)..."
println(k)
end
使用 SortedDict
时,请确保这样的排序有意义。在这个玩具示例中,d
的键是整数,并且它们具有 isless
和 isequal
的逻辑顺序;请参阅文档 here.
您可以使用 last
:
获取 SortedDict
的最后一个条目
using DataStructures
D = SortedDict(d)
last(D) ### 3=>4
使用Reverse
模块翻转顺序:
using Reverse
D2 = SortedDict(d, Reverse)
给定一个字典:
> d = Dict{Int, Int}(1=>123, 2=>51, 4=>23)
Dict{Int64,Int64} with 3 entries:
4 => 23
2 => 51
1 => 123
我可以通过键访问字典的值,例如:
> d[4]
23
或者我可以这样遍历键值对:
> for i in d
println(i)
end
4=>23
2=>51
1=>123
我试过将键作为列表的第一个元素访问,甚至 i.key
,但它似乎不是正确的语法:
julia> for i in d
println(i.key)
end
ERROR: type Pair has no field key
in macro expansion; at ./REPL[22]:2 [inlined]
in anonymous at ./<missing>:?
julia> for i in d
println(i[0])
end
ERROR: BoundsError: attempt to access 4=>23
at index [0]
in getindex(::Pair{Int64,Int64}, ::Int64) at ./operators.jl:609
in macro expansion; at ./REPL[23]:2 [inlined]
in anonymous at ./<missing>:?
然后我想起来 Julia 不是第 0 个索引,所以它应该是:
> for i in d
println(i[1], ' ', i[2])
end
4 23
2 51
1 123
> for i in d
println(i[1], ' ', i[2])
end
4 23
2 51
1 123
在这种情况下,当找不到列表的索引时,是不是有点像 Python 的 IndexError
?
问题的另一部分是关于SortedDict
,如何访问SortedDict
中的最后第N个元素?
我试过使用索引语法,我检索到了值,但没有检索到 (key,value)
的元组。
julia> import DataStructures: SortedDict
julia> sd = SortedDict(d)
DataStructures.SortedDict{Int64,Int64,Base.Order.ForwardOrdering} with 3 entries:
1 => 123
2 => 51
4 => 23
julia> sd[end]
23
另外,如何根据值对字典进行排序?
最后,如何反转排序后的字典?
我试过使用 Base.Order.ReverseOrding
但它抛出了一个 MethodError
:
julia> sd = SortedDict{Base.Order.ReverseOrdering}(d)
ERROR: MethodError: Cannot `convert` an object of type Dict{Int64,Int64} to an object of type DataStructures.SortedDict{Base.Order.ReverseOrdering,D,Ord<:Base.Order.Ordering}
This may have arisen from a call to the constructor DataStructures.SortedDict{Base.Order.ReverseOrdering,D,Ord<:Base.Order.Ordering}(...),
since type constructors fall back to convert methods.
in DataStructures.SortedDict{Base.Order.ReverseOrdering,D,Ord<:Base.Order.Ordering}(::Dict{Int64,Int64}) at ./sysimg.jl:53
我不是 Python 用户,而是 IndexError looks similar to the documentation for BoundsError 的文档。请注意,您始终可以使用 ?
查询当前的 Julia 文档,例如
?BoundsError
您可以对字典键和值进行排序:
d = Dict{Int,Int}(1 => 2, 3 => 4)
k = sort(collect(keys(d)) ### collect() forms an array that you can sort
v = sort(collect(values(d))
但我不明白您为什么要按值排序。为什么不简单地使用值作为键?
您可以轻松地遍历键或值:
for k in keys(d) ### or "for v in values(d)..."
println(k)
end
使用 SortedDict
时,请确保这样的排序有意义。在这个玩具示例中,d
的键是整数,并且它们具有 isless
和 isequal
的逻辑顺序;请参阅文档 here.
您可以使用 last
:
SortedDict
的最后一个条目
using DataStructures
D = SortedDict(d)
last(D) ### 3=>4
使用Reverse
模块翻转顺序:
using Reverse
D2 = SortedDict(d, Reverse)