python - 在 dict() 上使用 `bisect_left()` 会引发 `dict is not a sequence` 错误。如何遍历 dict() 以改进搜索过程时间
问题描述
这是我的代码。我正在尝试从 word 文件创建一个 dict 并将其用于搜索过程。我使用相同的方法bisect_left()
会引发序列错误lists
dict()
import bisect
fln = open("CROSSWD.TXT")
def create_dict(x):
new_dict=dict()
i=0
for line in x:
word=line.strip()
new_dict[word]= i
i+=1
return new_dict #create a new_dict
如何在字典上使用 bisect_left?
def search_dict(new_dict,s):
i= bisect_left(new_dict,s) #raises a sequence error. what other method can I use?
if s in new_dict[i]:
return True
else:
return False
s='zebra'
new_dict=create_dict(fln)
if search_dict(new_dict,s):
print(s," in dict")
else:
print(s," not in dict")
解决方案
bisect_left
需要一个支持整数索引的对象,因为二分算法需要知道“中间”元素是什么才能平分序列。C 实现bisect_left
令人困惑地提出了一个TypeError
类型对象dict
没有长度的声明。虽然这种说法是错误的,但最好dict
比纯 Python 版本(见下文)更早地检测和拒绝参数。
也就是说,不需要bisect
在 a 上使用dict
: adict
的目的是允许 O(1) 通过散列访问对象,而不是通过二进制搜索来搜索它 O(log n) 时间。
的纯 Python 版本bisect_left
,减去一些注释:
def bisect_left(a, x, lo=0, hi=None):
if lo < 0:
raise ValueError('lo must be non-negative')
if hi is None:
hi = len(a)
while lo < hi:
mid = (lo+hi)//2
if a[mid] < x: lo = mid+1
else: hi = mid
return lo
请注意,这len(a)
是在假设您可以稍后a
使用从长度派生的整数进行索引的情况下计算的。a 不是这样的dict
(或者如果你没有得到 a KeyError
,它肯定不会帮助你找到你的目标)。此外,一旦bisect
模块定义了纯 Python 函数,它就会尝试用从_bisect
. 这些功能显然会提前检查以确保a
不是dict
. 引发的错误消息有点误导。
推荐阅读
- swift - 我可以在 xcode 中使用谷歌地图和地点,我可以自定义图标吗?
- php - 视图中的 Laravel 集合搜索
- rspec - 来自gem的方法的chefspec中未定义的局部变量
- python - 导入多个 excel 文件,创建列并从 excel 文件名中获取值
- excel - 如果存在于另一个工作表中,则删除行
- javascript - 循环倒数计时器 - 基于实际时间每 10 分钟
- qt - QT:发布中的渲染与调试不同
- java - 根据每个字符串的子字符串对字符串 ArrayList 的一半进行排序
- angular - Angular - Rxjs Observable - 点击事件实现
- python - 正则表达式 - 获取数据直到第一次出现两个不同的序列