【发布时间】:2017-08-26 20:50:27
【问题描述】:
所以我有一个这样的列表字典:
dct = {'1': ['hello','goodbye'], '2': ['not here','definitely not here']}
检查“你好”是否在我的字典列表中的最快方法是什么
【问题讨论】:
-
any('hello' in val for val in dct.values()). -
非常感谢!如果您提交作为答案,我可以接受并投票给您
-
如果你颠倒数据结构,这会更有效率。
dct = { 'hello': 1, 'goodbye': 1, 'not here': 2, 'definitely not here': 2 }-- 这样它是一个恒定时间的搜索,而不是根据字典有多少项目而花费更长的时间。 -
@Charles:倒排字典不是免费的。
-
@martineau, ...此外,即使修改构建它的过程是不可行的,也可以将其反转一次并以任意便宜的方式搜索该反转结构次数。
标签: python list python-3.x dictionary