是否有一种有效的方法可以仅查找v In Inter P Inter P Inter的感兴趣的组合

发布于 2025-01-29 03:21:18 字数 516 浏览 3 评论 0原文

是否有一种有效的方法只能找到P Inted p Inter P中的V In Imption的组合,

我有类似的列表。

V = [2,3,5,7,9,12] 

V的大小可以从0到50 根据我的输入。 V没有重复。

我还有另一个列表p这样的 

P = [ [1],[1,2],[2,3] [3,4], [3,5,7], [3,5,8] ...]

p是恒定的,尺寸为1000。p

没有重复项,在p中,每个元素都按升序排序。 

P中找到V中存在的V组合的一种有效方法是

在 BE [[2,3],[3,5,7]

我已经尝试从V产生所有可能的安排,然后过滤  by  nbsp;这不是很高效。只是想知道是否有更好的方法可以做我要求的。 

既然我已经知道P是任何形式的树是一个更好的选择吗?

Is there a efficient way to find only the combinations of interest from V exciting in P

I have a list of something like this.

V = [2,3,5,7,9,12] 

Size of V can be anywhere from 0 to 50  based on my input.
V has no duplicates.

I have another list P something like this 

P = [ [1],[1,2],[2,3] [3,4], [3,5,7], [3,5,8] ...]

P is constant with size 1000.

P has no duplicates and within P, each element is sorted in ascending order. 

What would be an efficient way to find the combinations from V existing in P.

For example

When V = [2,3,5,7,9,12] combination of interest would be [[2,3],[3,5,7]].

I have already tried generating all possible arrangements from V and then filter by P.  This is not very efficient. Just wondering if there is a better way to do what I asked for. 

Since I already know P is any form of Tree a better option for this ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

难理解 2025-02-05 03:21:18

在实践中,使用set.issuperset可能是最快的方法:

v = set(V)
[c for c in P if v.issuperset(c)]

也可以使用两个指针,沿每个列表逐渐前进。在Python中可能会较慢,但在C/C ++中更快:

def is_subset(V, combination):
    # both V and combination are assumed sorted
    v = iter(V)
    for element in combination:
        for el in v:
            if el >= element:
                break
        if el != element:
            return False
    return True

[c for c in P if is_subset(V, c)]

Using set.issuperset is probably the fastest method in practice:

v = set(V)
[c for c in P if v.issuperset(c)]

It is also possible to use two pointers, gradually advancing along each list. It will probably be slower in Python, but faster in C/C++:

def is_subset(V, combination):
    # both V and combination are assumed sorted
    v = iter(V)
    for element in combination:
        for el in v:
            if el >= element:
                break
        if el != element:
            return False
    return True

[c for c in P if is_subset(V, c)]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文