python 算法: how to efficiently find if two integer sets are intersected?

标签 python algorithm data-structures mongodb

给定一个集合 [2004, 2008],找出这个集合是否与其他集合相交的最快方法是什么?

其实我在处理数据库的问题,表有2列,一列是下限,另一列是上限。任务是找到所有与给定的 2 元组相交的行(如 [2004,2008])。

我正在使用 mongodb,这是本质上支持的吗(我的意思是有关键字来做到这一点)。 我有庞大的用户群,所以我希望尽快完成这项任务。

编辑:为了更清楚地统计,数据库表包含以下行:

20 30
10 50
60 90
...

给定输入 (25 40) 范围,我想返回代表范围的行,与给定范围有交集。

所以返回是:(20 30),(10 50)

最佳答案

我根本不知道MongoDB,但你基本上是在寻找

SELECT * from the_table where not (lower_bound > 2008 or upper_bound < 2004) .

关于 python 算法: how to efficiently find if two integer sets are intersected?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4646089/

相关文章:

algorithm - 线段树第 K 个最大值

c - 归并排序程序出错

python - 列表元素的公平划分

algorithm - 搜索和匹配算法

c++ - 在无序元素上有效的唯一性

python - 复制.pdf、.html、.jpeg文件的查询方法

algorithm - 使用单词字典将字符串 a 转换为 b

python - 从 python 上传到 BigQuery 时,某些行出现 ASCII 0 错误

python - 多进程,多个进程读取同一个文件

Python单元测试测试是模拟错误的输入