- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个列表/元组的元组/列表(我使用哪个无关紧要),其中内部列表或元组的值具有可变大小。我需要检查变量是否在第一个插槽内部列表或元组中。
Structure looks like this:
[ [[list of x amount of ints],[same number of ints as first slot],[[y number of ints], [z number of ints], .... , [a number of ints]], ... repeated about 20x]
例子:
([1, 21, 54, 55, 93, 99, 284, 393, 964, 1029, 1214, 1216, 1223, 1253, 1258, 1334, 1365, 1394, 1397, 1453, 1471, 1543, 1589, 1824, 1975, 2054, 2090, 2164, 2165, 2166, 2167, 2323, 2547, 2645, 2802, 2809, 2931, 2958, 3031, 3071, 3077, 3078, 3189, 3199, 3202, 3203], [1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 2, 1, 1, 2, 1, 1, 1, 2, 4, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 4, 2, 1, 1, 1, 1, 1, 2, 3, 1, 2, 1, 3, 3, 1, 2], [[3], [1], [4], [2], [12], [6], [3], [8], [20, 27], [11], [4, 7], [71], [133], [14, 74], [6], [67], [34], [3, 16], [9, 7, 23, 71], [11, 43], [67], [71], [4], [139], [16], [52], [4], [31], [7, 50], [2, 12], [1, 1, 83, 114], [13, 70], [60], [121], [30], [16], [214], [29, 78], [9, 37, 60], [14], [216, 249], [28], [2, 2, 21], [4, 18, 22], [59], [8, 24]])
This is just the first value of my list of 20k+ elements that is similar.
所以我有一个函数来检查一个数字是否在:
[1, 21, 54, 55, 93, 99, 284, 393, 964, 1029, 1214, 1216, 1223, 1253, 1258, 1334, 1365, 1394, 1397, 1453, 1471, 1543, 1589, 1824, 1975, 2054, 2090, 2164, 2165, 2166, 2167, 2323, 2547, 2645, 2802, 2809, 2931, 2958, 3031, 3071, 3077, 3078, 3189, 3199, 3202, 3203]
它会返回索引。
我的功能:iD 是我要查找的数字,而 posting 只是我嵌套循环中的第一个元素(上面的直接 block 是 posting 的示例)
def searchCurrentPosting(iD,posting):
x = 0
for each in posting[0]:
if iD == each:
return x
x += 1
return False
每次给出一个新词(某个给定数字的 20k 次方)时,我都必须运行此搜索功能。此代码将运行大约一分钟。无论如何要缩短时间?
编辑:如果你想要我的整个程序,它是:
这是我的主要驱动程序:http://pastebin.com/Udjit7PP
它解析的文件是:CACM 集合,这是 IR 测试的标准。
使用词干提取(端口词干提取):http://pastebin.com/AzA0fvdV
是的,我正在创建倒排索引。
最佳答案
由于索引 0 处的列表已排序,因此您可以使用 bisect
模块在 O(log N)
时间内找到索引:
In [33]: import bisect
In [34]: lst = [1, 21, 54, 55, 93, 99, 284, 393, 964, 1029, 1214, 1216, 1223, 1253, 1258, 1334, 1365, 1394, 1397, 1453, 1471, 1543, 1589, 1824, 1975, 2054, 2090, 2164, 2165, 2166, 2167, 2323, 2547, 2645, 2802, 2809, 2931, 2958, 3031, 3071, 3077, 3078, 3189, 3199, 3202, 3203]
In [35]: n = 2802
In [36]: ind = bisect.bisect_left(lst, n)
In [37]: if lst[ind] == n:
...: print 'Item found at {}'.format(ind)
...:
Item found at 34
请注意,如果列表未排序,那么最好先对其进行排序并将引用存储在变量中,这样您就不必一次又一次地对其进行排序。
另一种选择是使用字典,将项目作为键,索引作为值(对于重复的项目,只存储它们第一次出现的索引,即类似于 list.index
)。创建字典后,您可以在 O(1)
时间内获取项目的索引。
In [38]: dct = {}
In [39]: for i, x in enumerate(lst):
...: if x not in dct:
...: dct[x] = i
...:
In [40]: dct.get(n)
Out[40]: 34
In [41]: dct.get(1000) #return None for non-existent items
时序比较:
In [43]: lst = list(range(10**5))
In [44]: %timeit bisect.bisect_left(lst, 10**5-5)
1000000 loops, best of 3: 444 ns per loop
In [45]: %timeit lst.index(10**5-5)
1000 loops, best of 3: 1.29 ms per loop
In [46]: %timeit dct.get(10**5-5) #dct created using the new list.
10000000 loops, best of 3: 104 ns per loop
如果您连续更新索引为 0 的列表并且它没有排序,那么您应该简单地使用 list.index()
而不是使用循环、字典或一分为二。
In [47]: try:
...: ind = lst.index(n)
...: print 'Item found at {}'.format(ind)
...: except IndexError:
...: pass
...:
Item found at 34
关于返回索引的元组/列表列表中的 Python 最快搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26147539/
这几天我一直在努力。我一直在自学 CSS,所以对菜鸟好一点。我正在创建一个推荐 slider 。推荐以 3 个 block 显示。我希望前 2 个下降,第 3 个上升。但是当 slider 激活时,无
我最近开始学习 Nodejs,现在我很困惑我的网络应用程序使用什么,html 还是 ejs (Express)。 Ejs 使用 Express 模块,而 .html 使用 HTML 模块。我的第一个问
假设我们有一个 PostgreSQL 表contacts,每条记录都有一堆带标签的电子邮件地址(标签和电子邮件对)——其中一个是“主要”。 存储方式如下: id 主键 电子邮件 文本 email_la
我成功为一种新的tesseract语言编写了traineddata文件,但是当我完成时,我继续收到以下错误: index >= 0 && index = 0 && 索引 < size_used_ :E
这个问题已经有答案了: How to deal with SettingWithCopyWarning in Pandas (21 个回答) 已关闭 4 年前。 假设我有一个像这样的数据框,第一列“密
如果我有一个位置或行/列同时用于 A 和 B 位置,请检查 B 是否与 A 成对角线? 1 2 3 4 5 6 7 8 9 例如,我如何检查 5 是否与 7 成对角线? 此外,如果我检查 4 是
MongoDB:索引 一、 创建索引 默认情况下,集合中的_id字段就是索引,我们可以通过getIndexes()方法来查看一个集合中的索引 > db.user.getIndexes() [ { "v
一、索引介绍 索引是一种用来快速查询数据的数据结构。 B+Tree就是一种常用的数据库索引数据结构,MongoDB采用B+Tree 做索引,索引创建在colletions上。 MongoDB不使用索引
我无法决定索引。 就像我有下面的查询需要太多时间来执行: select count(rn.NODE_ID) as Count, rnl.[ISO_COUNTRY_CODE] as Cou
我有这些表: CREATE TABLE `cstat` ( `id_cstat` bigint(20) NOT NULL, `lang_code` varchar(3) NOT NULL,
我正在尝试找到一种方法来提高包含 IP 范围的 mysql 表的性能(在高峰时段每秒最多有 500 个 SELECT 查询(!),所以我有点担心)。 我有一个这种结构的表: id smallint(
jquery index() 似乎无法识别元素之一,总是说“无法读取未定义的属性‘长度’”这是我的代码。mnumber 是导致问题的原因。我需要 number 和 mnumber 才能跟踪使用鼠标,并
我们有一个包含近 4000 万条记录的 MongoDB 集合。该集合的当前大小为 5GB。此集合中存储的数据包含以下字段: _id: "MongoDB id" userid: "user id" (i
文档说:如果你有多个字段的复合索引,你可以用它来查询字段的开始子集。所以如果你有一个索引一个,乙,丙你可以用它查询一种一个,乙a,b,c 我的问题是,如果我有一个像这样的复合索引一个,乙,丙我可以查询
我正在使用 $('#list option').each(function(){ //do stuff }); 循环列表中的选项。我想知道如何获取当前循环的索引? 因为我不想让 var i = 0;循
MySQL索引的建立对于MySQL的高效运行是很重要的,索引可以大大提高MySQL的检索速度。 打个比方,如果合理的设计且使用索引的MySQL是一辆兰博基尼的话,那么没有设计和使用索引的MySQL
SQLite 索引(Index) 索引(Index)是一种特殊的查找表,数据库搜索引擎用来加快数据检索。简单地说,索引是一个指向表中数据的指针。一个数据库中的索引与一本书后边的索引是非常相似的。
我是 RavenDB 的新手。我正在尝试使用多 map 索引功能,但我不确定这是否是解决我的问题的最佳方法。所以我有三个文件:Unit、Car、People。 汽车文件看起来像这样: { Id: "
我有以下数据,我想根据范围在另一个表中建立索引 我想要实现的是,例如,如果三星的销售额为 2500,则折扣为 2%,低于 3000 且高于 1000 我知道它可以通过索引来完成,与多个数组匹配,然后指
我正在检查并删除 SQL 数据库中的重复和冗余索引。 所以如果我有两个相同的索引,我会删除。 例如,如果我删除了重叠的索引... 索引1:品牌、型号 指标二:品牌、型号、价格 我删除索引 1。 相同顺
我是一名优秀的程序员,十分优秀!