hashmap - 是否有允许搜索一系列键的 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
推荐阅读
- java - 使用 multipart 上传多个图像时出错
- java - 如何从 DSpace-CRIS 中的不同元数据中收集有关 CRIS 实体的数据?
- c - Libcurl:无法连接到 www.google.fr 连接被拒绝
- angular - 如何为 Angular SPA 实现缓存
- php - 我需要在另一个选项卡的 echo 语句中打开一个文件
- javascript - 请求谷歌图书 API 不正确
- javascript - 在 node.js 中导入 sequelize 模型的最佳方法
- java - 如何用int = 1随机填充10%的二维数组?
- azure - Microsoft Groups Graph API Multi-Geo:有没有办法使用应用程序权限(AAD 应用程序)在指定的地理位置创建组
- android - 如果我有图像的内容 uri,如何传递图像文件?