有序子集测试 [英] Ordered subsets test

查看:36
本文介绍了有序子集测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想测试一个有序集合是否是一个更大的有序集合的子集.我使用了元组和itertools.combinations:

I want to test if an ordered set is a subset of a bigger ordered set. I used tuples and itertools.combinations:

def subset_test(a, b):
    return a in itertools.combinations(b, len(a))

例如,

>>> subset_test((0, 1, 2), (0, 3, 1, 4, 2))
True
>>> subset_test((0, 1, 2), (0, 3, 2, 4, 1))
False

它可以工作,但是在测试大元组时很慢.

It works, but is slow when I test big tuples.

推荐答案

您可以简单地使用迭代器来跟踪B中的位置

You can simply use an iterator to keep track of the position in B

>>> A = (0, 1, 2)
>>> B = (0, 3, 1, 4, 2)
>>> b_iter = iter(B)
>>> all(a in b_iter for a in A)
True

这篇关于有序子集测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆