在Python字典中查找所有键> = integer

som*_*ike 0 python dictionary for-loop key

说我有以下字典时间密钥:

dict = {
  "1363017884": "some val",
  "1363033813": "another val",
}
Run Code Online (Sandbox Code Playgroud)

我想找到比1363033000更大的所有键(在这种情况下只有1363033813匹配).我有一个for循环检查每个键,但这似乎效率低下:

for epoch,value in dict.iteritems():
  if int(epoch) >= 1363033000:
    do something interesting
Run Code Online (Sandbox Code Playgroud)

Mar*_*ers 7

在字典上循环是唯一真正的选择,没有更有效的方法.

或者您可以使用不同的数据结构; 例如,存储连接到btree结构中的值的整数将使搜索大于或低于给定搜索值的键更加有效.