- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有几个列表,每个列表包含几个城市。我需要检查任意两个随机元素是否属于同一个列表。
简单的例子:
list1 = ['London', 'Manchester', 'Liverpool', 'Edimburgh']
list2 = ['Dublin', 'Cork', 'Galway']
list3 = ['Berlin', 'Munich', 'Frankfurt', 'Paris', 'Milan', 'Rome', 'Madrid', 'Barcelona', 'Lisbon', ...]
list4 = ['Washington', 'New York', 'San Francisco', 'LA', 'Boston', ...]
预期结果:
> in_same_group('London', 'Liverpool')
> True
>
> in_same_group('Berlin', 'Washington')
> False
该函数被频繁调用,因此速度很关键。最大的列表最多可能包含 1000 个元素。
最有效的方法是什么?
这是我到目前为止尝试过的方法,但是速度太慢了:
def in_same_group(city1, city2):
same_group = False
for this_list in [list1, list2, list3...]:
if city1 in this_list and city2 in this_list:
return True
return same_group
最佳答案
这是 Horia 的 proposal 的组合和我原来的。您可以定义一个 dict
,其中城市作为键,索引集作为值:
list1 = ['London', 'Manchester', 'Liverpool', 'Edimburgh']
list2 = ['Dublin', 'Cork', 'Galway', 'Paris', 'Rome']
list3 = ['Berlin', 'Munich', 'Frankfurt', 'Paris', 'Milan', 'Rome', 'Madrid', 'Barcelona', 'Lisbon']
list4 = ['Washington', 'New York', 'San Francisco', 'LA', 'Boston']
# Note that 'Paris' and 'Rome' are both in list2 and list3
groups = [list1, list2, list3, list4]
indices = {}
for i, group in enumerate(groups):
for city in group:
indices.setdefault(city, set()).add(i)
结构紧凑,如下所示:
print(indices)
#{'London': {0}, 'Manchester': {0}, 'Liverpool': {0}, 'Edimburgh': {0}, 'Dublin': {1}, 'Cork': {1}, 'Galway': {1}, 'Paris': {1, 2}, 'Rome': {1, 2}, 'Berlin': {2}, 'Munich': {2}, 'Frankfurt': {2}, 'Milan': {2}, 'Madrid': {2}, 'Barcelona': {2}, 'Lisbon': {2}, 'Washington': {3}, 'New York': {3}, 'San Francisco': {3}, 'LA': {3}, 'Boston': {3}}
由于 set intersection,对于任何城市对,您可以获得一组通用索引:
def common_groups(city1, city2):
return indices.get(city1, set()) & indices.get(city2, set())
print(common_groups('London', 'Liverpool'))
# {0}
print(common_groups('London', 'Paris'))
# set()
print(common_groups('Cork', 'Paris'))
# {1}
print(common_groups('Rome', 'Paris'))
# {1, 2}
print(common_groups('Rome', 'Nowhere'))
# set()
空集在 Python 中是错误的。
对于 n
个城市,创建字典的时间为 O(n)
,空间要求为 O(n)
,查找性能为为 O(1)
。作为奖励,查询不仅返回 bool 值,还返回一组索引。
最后,由于设置了交叉点,如果您想检查三个或更多城市是否在同一组中,此方法也适用。
关于python - 如何判断两个元素是否属于同一个列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47416917/
判断置顶文章 is_sticky() 函数用来判断一篇文章是否为置顶文章。 用法 ?
判断结构要求程序员指定一个或多个要评估或测试的条件,以及条件为真时要执行的语句(必需的)和条件为假时要执行的语句(可选的)。 下面是大多数编程语言中典型的判断结构的一般形式: 判断语句 C
我经常这样写: (if (nil? a-value) another-value a-value) 是否有更简单的功能可用,例如: (if-nil? a-value another-value) 最佳
MySQL IF 语句允许您根据表达式的某个条件或值结果来执行一组 SQL 语句。 要在 MySQL 中形成一个表达式,可以结合文字,变量,运算符,甚至函数来组合。表达式可以返回 TRUE,FA
也就是说,是否有一种工具可以自动显示给定语法的完整语言,包括突出歧义(如果有)? 最佳答案 BNF 风格的文法可能有一些特殊性,但总的来说,确定给定的上下文无关文法(例如 BNF)是否有歧义是不可能的
有没有办法确定像下面这样的 Axios 请求是否收到了答案并完成了? axios.get('/api') .then(response => this.data = response.data); 最
我想请大家禁用 Firebug 。如何确定自己安装了firebug?所以它是一个跨浏览器,并在 Chrome、Mozilla 和 IE8 + 中确定 最佳答案 两步: 如果 window.consol
我有一个看起来像这样的对象: var searchFilter = {_id: XXX, approved: true} 用于驱动 Meteor 集合搜索过滤器。然后,我有一对文本框,允许用户输入一系
我正在循环并向我的数据库中插入几百万条记录。性能是第一要务。 我想利用无状态 session ,但您可能知道它们不支持在更复杂的实体上级联对象。 是否有一种通用方法可以确定实体是否具有级联记录?如果是
我正在使用 pdfminer 解析一些 PDF 文件。图书馆。 我需要知道文档是否是扫描文档,扫描机将扫描图像放在顶部,将 OCR 提取的文本放在背景中。 有没有办法识别文本是否可见,因为 OCR 机
我正在寻找一种方法来找出当前为浏览器游戏 TribalWars 编写的脚本打开的页面。 URL 的设置非常相似,对于知道自己在做什么的人来说这应该很容易(我显然不知道)。 URL 如下所示: http
我在 C# 中使用包装的 C 库,需要将图像从该库转换为位图并返回,但没有复制像素缓冲区。 转换为位图很简单: Bitmap WrapAsBitmap(CImage image) { retu
有没有办法检查调用方法的Controller是否来自Area内的Controller? 例如,我有一个继承自 AuthorizeAttribute 的类,例如 public class CustomA
是否可以找到MySQL View 中某列所属的表名? 如果 View 构造为 CREATE VIEW alpha_view AS SELECT alpha.col1, alpha.col2,
如何判断 .Net 应用程序是作为桌面应用程序运行还是作为服务运行? 我们正在尝试使用 Fitnesse 测试我们的应用程序,它将应用程序作为服务加载,然后调用它。但是当一个模式错误框被按下时,它就会
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求提供代码的问题必须表现出对所解决问题的最低限度理解。包括尝试过的解决方案、为什么它们不起作用,以及
我试图计算出 iframe 内容的大小,以便调整 iframe 元素的大小以包含其内容。 如何确定 iFrame 是否已加载以及我是否可以可靠地测量它的内容尺寸。 注意:onload 事件不会执行,因
这个问题在这里已经有了答案: How to write portable code in c++? (12 个答案) 关闭 9 年前。 我正在尝试编写可以用任何现代版本的 g++ 编译的代码,但遇到
这个问题在这里已经有了答案: distinguish shared objects from position independent executables (2 个答案) 关闭 4 年前。 我有
我的目标是如果 dte 与当前时间相差不到 1 小时,则停止循环。是否有“ ruby 方式”来做到这一点? #THIS IS AN INFINITE LOOP, DONT RUN THIS dte=D
我是一名优秀的程序员,十分优秀!