Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 方法生成唯一的数字组合_Python - Fatal编程技术网

Python 方法生成唯一的数字组合

Python 方法生成唯一的数字组合,python,Python,我有一个数组: A = [0,2,5,6] B = [5,6,8,9] C = [6,7,8,9] 我想写两个函数,具体如下: 问题1 当我传入上面定义的任何数组时,我按顺序意义n+1以顺序方式得到数字的组合。因此,期望的输出是: ResultA = [[0],[2],[5],[6],[5,6]] ResultB = [[5],[6],[8],[9],[5,6],[8,9]] ResultC = [[6],[7],[8],[9],[6,7],[7,8],[8,9],[6,7,8],[7,8,9

我有一个数组:

A = [0,2,5,6]
B = [5,6,8,9]
C = [6,7,8,9]
我想写两个函数,具体如下:

问题1 当我传入上面定义的任何数组时,我按顺序意义n+1以顺序方式得到数字的组合。因此,期望的输出是:

ResultA = [[0],[2],[5],[6],[5,6]]
ResultB = [[5],[6],[8],[9],[5,6],[8,9]]
ResultC = [[6],[7],[8],[9],[6,7],[7,8],[8,9],[6,7,8],[7,8,9],[6,7,8,9]]
以下是我尝试过的:

sorted_ids = sorted(number_collection)
combinations = [sorted_ids[j: j + i] for i in range(1, len(sorted_ids)) for j in range(len(sorted_ids) - i + 1)]
问题是它适用于数组C,但不适用于其他数组

问题2 问题1的结果是此问题的输入。问题是我想要存在于数字的唯一元素上的组合。我不确定我是否能用文字正确解释,以下是所需的输出:

FinalResultA = [[0],[2],[5,6]]
FinalResultB = [[5,6],[8,9]]
FinalResultC = [[6,7,8,9]]

是否有任何面向性能的方法可以帮助我?

这是一种相当有效的方法,尽管它确实需要辅助空间,但如果运行次数较少,则不应太多:

from itertools import groupby

def ngrams(seq):
    stop = len(seq)+1
    for n in range(2, stop):
        for i in range(stop - n):
            yield seq[i:i+n]

def get_combos(seq):
    runs = []
    for _, g in groupby(enumerate(seq), lambda x:x[1]-x[0]):
        run = [a for _, a in g]
        for x in run:
            yield [x]
        if len(run) > 1:
            runs.append(run)
    for run in reversed(runs):
        yield from ngrams(run)
注意,这用于对连续整数进行分组。它在连续整数组上迭代、运行并生成任何单个整数作为单个元素列表。如果运行时间长于1,我会将其添加到运行列表中。最后,以相反的顺序迭代运行列表,生成从顺序2到顺序lenrun的n-gram

在行动中:

>>> A = [0,2,5,6]
>>> B = [5,6,8,9]
>>> C = [6,7,8,9]
>>> list(get_combos(A))
[[0], [2], [5], [6], [5, 6]]
>>> list(get_combos(B))
[[5], [6], [8], [9], [8, 9], [5, 6]]
>>> list(get_combos(C))
[[6], [7], [8], [9], [6, 7], [7, 8], [8, 9], [6, 7, 8], [7, 8, 9], [6, 7, 8, 9]]
注意:get_组合假定输入已排序

但是,请编辑以下内容:

>>> D = [6,7,9,12,13,14,20,21,30]
这将产生:

>>> list(get_combos(D))
[[6], [7], [9], [12], [13], [14], [20], [21], [30], [20, 21], [12, 13], [13, 14], [12, 13, 14], [6, 7]]
即,3序列在后续运行的2序列产生之前开始。如果希望在n+1 len序列之前生成所有n-len序列,请使用以下方法:

from itertools import groupby

def ngrams(seq, max_len):
    curr = seq
    for n in range(1, max_len + 1):
        nxt = []
        for run in curr:
            run_len = len(run)
            if run_len > n:
                nxt.append(run)
            for i in range(run_len + 1 - n):
                yield run[i:i+n]
        curr = nxt

def _sub_index(t):
    return t[1] - t[0]

def get_consecutive_runs(seq):
    grouped = groupby(enumerate(seq), _sub_index)
    for _, g in grouped:
        yield [a for _, a in g]


def get_combos(seq):
    runs = list(get_consecutive_runs(seq))
    max_len = max(map(len, runs))
    yield from ngrams(runs, max_len)
结果如下:

>>> list(get_combos(D))
[[6], [7], [9], [12], [13], [14], [20], [21], [30], [6, 7], [12, 13], [13, 14], [20, 21], [12, 13, 14]]

以下是一个函数中的两种解决方案,无需任何外部库:

A = [0,2,5,6]
B = [5,6,8,9]
C= [6,7,8,9]

def finding_sequence(list_1):
    sub_list = []
    for j, i in enumerate(list_1):

        try:
            if list_1[j] - list_1[j - 1] == 1:
                sub_list.append((list_1[j - 1], list_1[j]))
            else:
                sub_list.append('_pos')


        except IndexError:

            pass

    sub_final_result = []
    check_result=[]
    if '_pos' not in sub_list[1:]:
        for i in sub_list[1:]:
            for k in i:
                if k not in sub_final_result:
                    sub_final_result.append(k)
                    check_result.append(k)

    else:
        for i in sub_list:
            if i != '_pos':
                sub_final_result.append(i)
                for i1 in i:
                    check_result.append(i1)


    for i1 in list_1:
        if i1 not in check_result:
            sub_final_result.append([i1])

    return sub_final_result
测试用例:

print(finding_sequence(A))
输出:

[(5, 6), [0], [2]]
[(5, 6), (8, 9)]
第二

print(finding_sequence(B))
输出:

[(5, 6), [0], [2]]
[(5, 6), (8, 9)]

附言:一个要求:如果我的答案对你有帮助,不要接受它,直接使用它。

输入总是被排序的吗?排序可以吗?排序完全可以。为什么结果不包括[0,2],[0,2,5],[0,2,5,6],[2,5,6]。@thefourtheye发布的输出是期望的输出,而不是来自我的方法的输出。这种方法的问题是[0,2]等的存在,所以最后你要寻找一个连续数的分区?这意味着所有列表成员都是整数和唯一的,对吗?中间步骤问题1是否是一项要求,即其输出是否将用于其他用途?@juanparrivillaga能否请您分享我将如何使用这两个函数?@iam.Carrot i justdid@juanpaarrivillaga,非常感谢。关于问题2?我能解决这个问题吗?那会很有意义的help@iam.Carrot我还是不太明白你到底想要解决问题2。看起来您只是想将原始列表中的连续整数分组。。。