hashmap - 是否有允许搜索一系列键的 julia 结构?

标签 hashmap range julia

我知道我可以对哈希图使用整数键,就像下面的字典示例一样。但是字典是无序的,不能从整数键中受益。

julia> hashmap = Dict( 5 => "five", 9 => "nine", 16 => "sixteen", 70 => "seventy")
Dict{Int64,String} with 4 entries:
  9  => "nine"
  16 => "sixteen"
  70 => "seventy"
  5  => "five"

julia> hashmap[9]
"nine"

julia> hashmap[8:50] # I would like to be able to do this to get keys between 8 and 50 (9 and 16 here)
ERROR: KeyError: key 8:50 not found
Stacktrace:
 [1] getindex(::Dict{Int64,String}, ::UnitRange{Int64}) at ./dict.jl:477
 [2] top-level scope at REPL[3]:1
我正在寻找一种有序结构,允许访问一定范围内的所有键,同时由于排序键而受益于性能优化。

最佳答案

有一个名为 DataStructures 的专用库其中有一个 SortedDict结构和相应的搜索功能:

using DataStructures
d = SortedDict(5 => "five", 9 => "nine", 16 => "sixteen", 70 => "seventy")

st1 = searchsortedfirst(d, 8)   # index of the first key greater than or equal to 8
st2 = searchsortedlast(d, 50)  # index of the last key less than or equal to 50
现在:
julia> [(k for (k,v) in inclusive(d,st1,st2))...]
3-element Array{Int64,1}:
  9
 16

关于hashmap - 是否有允许搜索一系列键的 julia 结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64889700/

相关文章:

list - 长的 scala 范围

python - 为 PyJulia 编译和使用自定义系统镜像

c - 查找任何范围的序列

java - 处理映射、equals() 和 hashCodes()。这有多有效?

Java HashObjObjMap<K, V> 与 HashMap<K, V>

java - 在ArrayList中显示0的词频

mysql - 试图查看数据点是否属于 SQL 的多个日期范围之一

julia - 在 Julia 中限定冲突函数

julia - 如何获得Julia中索引列表的补充?

java - 使用 HashMap 中的对象作为 Key