在 Python 中生成部分子集 [英] Generate partial subsets in Python

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

问题描述

忏悔:这是一个班级.

我正在尝试生成长度为 1 - 长度(完整集)的所有子集,但它们必须按顺序排列.

I am trying to generate all subsets of length 1 - length(full set), but they must be in order.

因此,输入 (4,2) 时,有效结果将是(4)、(4,2) 和 (2).不会是(4,4) (2,2) 或 (2,4)

So, with an input of (4,2), valid results would be (4), (4,2), and (2). Would not be (4,4) (2,2) or (2,4)

eta(4,2,2) 也应该返回 (2,2).

eta (4,2,2) should return (2,2) as well.

长度不是预先确定的.

我现在拥有的:

def gen_all_partials(outcomes, length):
    """
    Iterative function that enumerates the set of all sequences of
    outcomes of lengths up to given length.
    """

    answer_set = set([()])
    for dummy_idx in range(length):
        temp_set = set()
        for partial_sequence in answer_set:
            for item in outcomes:
                new_sequence = list(partial_sequence)
                new_sequence.append(item)
                temp_set.add(tuple(new_sequence))
        answer_set = temp_set
    return answer_set

这是由给定函数部分获得的.我不明白 Python 如何在第二个for"循环中迭代一个空集.除了正确"答案之外,当前代码还输出 (4,4)、(2,2) 和 (2,4).

This is partially obtained by a given function. I don't understand how Python is iterating over an empty set in that 2nd "for" loop. This current code outputs (4,4), (2,2), and (2,4) in addition to the "right" answers.

我被嵌套循环挂起并跟踪多个计数器,并为所需的输出设置不同的长度.

I'm getting hung up on the nested loops and keeping track of multiple counters and having different lengths for the desired output.

我也试过这个:

def gen_all_partials(outcomes, length):
    """
    Iterative function that enumerates the set of all sequences of
    outcomes of lengths up to given length.
    """

    answer_set = set([()])
    for dummy_idx in range(length):
        temp_set = set()
        for partial_sequence in answer_set:
            for item in outcomes:
                new_sequence = list(partial_sequence)
                new_sequence.append(item)
                if new_sequence not in temp_set:
                    temp_outcomes = list(outcomes[:])
                    add_to_set = True
                    for val in new_sequence:
                        if val in temp_outcomes:
                            order_list = []
                            for dummy_bit in val:
                                order_list.append(val.index(dummy_bit)) 
                                if order_list == order_list.sort():
                                    temp_outcomes.remove(val)
                                else:
                                    add_to_set = False
                        else: 
                            add_to_set = False
                    if add_to_set:
                        temp_set.add(tuple(new_sequence))
        answer_set = temp_set
    return answer_set

推荐答案

这是一种使用递归的方法,但您也可以使用循环迭代地进行,并加入每个 n 的列表:

Here's one way, using recursion, though you can alternatively do it iteratively with a loop, and join the lists for each n:

a = [4,2]

def subseqs(x):
    def go(x, n):
        return zip(*(x[i:] for i in range(n))) + go(x, n-1) if n else []
    return go(x, len(x))

print subseqs(a)   # [(4, 2), (4,), (2,)]

或使用列表推导式:

print sum([zip(*(a[i:] for i in range(n))) for n in range(len(a)+1)], [])

这篇关于在 Python 中生成部分子集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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