Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.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,python中的元组数组如下所示: [(1,2),(2,3),(2,4),(2,5),(2,6),(3,1),(3,2),(3,4)] 我需要生成给定长度的所有组合,但有一些条件。元组中应该总是只有2个相同的数字 对于这个例子,当开始生成组合时,我需要添加(1,2),然后添加(2,3),但是我不能添加(2,4),因为数字2在[(1,2),(2,3)]中已经使用了2次,所以我需要跳过或删除start数组中的这些项(其中包含数字2),(2,4),(2,5),(2,6),(3,2)并继续生成。给定长

python中的元组数组如下所示:

[(1,2),(2,3),(2,4),(2,5),(2,6),(3,1),(3,2),(3,4)]
我需要生成给定长度的所有组合,但有一些条件。元组中应该总是只有2个相同的数字

对于这个例子,当开始生成组合时,我需要添加
(1,2)
,然后添加
(2,3)
,但是我不能添加
(2,4)
,因为数字
2
[(1,2),(2,3)]
中已经使用了2次,所以我需要跳过或删除start数组中的这些项(其中包含数字2),
(2,4),(2,5),(2,6),(3,2)
并继续生成。给定长度3的第一个组合将是
[(1,2)、(2,3)、(3,1)]
,第二个
[(1,2)、(2,3)、(3,4)]
,然后
[(1,2)、(2,4)、(3,1)]
等等

有人能帮我怎么做吗

from random import sample

# Define variables

# All possible elements
all_elements = [1,2,3,4,5,6,7]

# Current possible free elements
possible_elements = {e: 0 for e in all_elements}

# Max number of element usage
MAX = 2

# Result length
N = 5

# Tuple length
L = 2

# Result
result = []

for _ in range(N):
    # Check if we can't construct a new tuple
    try:
        # Randomly get free elements
        t = sample(possible_elements.keys(), L)
    except ValueError:
        break
    # Add tuple to result
    result.append(t)
    # Check elements
    for element in t:
        # List for delete non-free elements
        elements_to_delete = []
        # Check if we can't use the element (it is filled to MAX)
        possible_elements[element] += 1
        if possible_elements[element] == MAX:
            # If yes, add it to to-delete list
            elements_to_delete.append(element)
        # Delete all non-free elements
        for e in elements_to_delete:
            del possible_elements[e]
result
返回:


[[1,6],[6,4],[3,5],[1,3],[7,5]

有几种方法可以做到这一点(其中大多数更有效),但只要原始元组数组很短,我认为您应该坚持概念上最简单的方法。这可能是在3个for循环中创建所有排列(从元组数组中取I,j,k)(使用if条件检查i、j和k是否不相同)并将其存储在字典中(键可能是从数组中分离出来的索引)。 最后检查字典是否有任何禁止的组合(使用了2…)并删除它们。
当然,您也可以结合最后两个步骤,但如果原始数组要长得多,则速度会慢得多。

我首先使用生成一个迭代器,用于元组上长度为3的排列。 然后我通过计算3个元组的子列表中每个元组的所有频率,如果元素的频率超过2,我不会将该子列表添加到最终列表中

import itertools as it
from collections import Counter

li = [(1,2),(2,3),(2,4),(2,5),(2,6),(3,1),(3,2),(3,4)]
perms = []

#Generate all permutations of length 3
for l in it.permutations(li, 3):
    flag = True
    counters = Counter()
    #Iterate through tuple and calculate frequency of each digit via Counter
    #Summing up counters for each tuple
    for tup in l:
        c = Counter(tup)
        counters += c
    #Iterate through the frequency dict, and if a value is above 2, don't add that tuple
    for v in dict(counters).values():
        if v > 2:
            flag = False
    if flag:
        perms.append(l)

print(perms)
输出将如下所示

[((1, 2), (2, 3), (3, 1)), ((1, 2), (2, 3), (3, 4)), ((1, 2), (2, 4), (3, 1)), ((1, 2), (2, 4), (3, 4)), ((1, 2), (2, 5), (3, 1)), ((1, 2), (2, 5), (3, 4)), ((1, 2), (2, 6), (3, 1)), ((1, 2), (2, 6), (3, 4)), ((1, 2), (3, 1), (2, 3)), ((1, 2), (3, 1), (2, 4)), ((1, 2), (3, 1), (2, 5)), ((1, 2), (3, 1), (2, 6)), ((1, 2), (3, 1), (3, 2)), ((1, 2), (3, 1), (3, 4)), ((1, 2), (3, 2), (3, 1)), ((1, 2), (3, 2), (3, 4)), ((1, 2), (3, 4), (2, 3)), ((1, 2), (3, 4), (2, 4)), ((1, 2), (3, 4), (2, 5)), ((1, 2), (3, 4), (2, 6)), ((1, 2), (3, 4), (3, 1)), ((1, 2), (3, 4), (3, 2)), ((2, 3), (1, 2), (3, 1)), ((2, 3), (1, 2), (3, 4)), ((2, 3), (2, 4), (3, 1)), ((2, 3), (2, 4), (3, 4)), ((2, 3), (2, 5), (3, 1)), ((2, 3), (2, 5), (3, 4)), ((2, 3), (2, 6), (3, 1)), ((2, 3), (2, 6), (3, 4)), ((2, 3), (3, 1), (1, 2)), ((2, 3), (3, 1), (2, 4)), ((2, 3), (3, 1), (2, 5)), ((2, 3), (3, 1), (2, 6)), ((2, 3), (3, 4), (1, 2)), ((2, 3), (3, 4), (2, 4)), ((2, 3), (3, 4), (2, 5)), ((2, 3), (3, 4), (2, 6)), ((2, 4), (1, 2), (3, 1)), ((2, 4), (1, 2), (3, 4)), ((2, 4), (2, 3), (3, 1)), ((2, 4), (2, 3), (3, 4)), ((2, 4), (2, 5), (3, 1)), ((2, 4), (2, 5), (3, 4)), ((2, 4), (2, 6), (3, 1)), ((2, 4), (2, 6), (3, 4)), ((2, 4), (3, 1), (1, 2)), ((2, 4), (3, 1), (2, 3)), ((2, 4), (3, 1), (2, 5)), ((2, 4), (3, 1), (2, 6)), ((2, 4), (3, 1), (3, 2)), ((2, 4), (3, 1), (3, 4)), ((2, 4), (3, 2), (3, 1)), ((2, 4), (3, 2), (3, 4)), ((2, 4), (3, 4), (1, 2)), ((2, 4), (3, 4), (2, 3)), ((2, 4), (3, 4), (2, 5)), ((2, 4), (3, 4), (2, 6)), ((2, 4), (3, 4), (3, 1)), ((2, 4), (3, 4), (3, 2)), ((2, 5), (1, 2), (3, 1)), ((2, 5), (1, 2), (3, 4)), ((2, 5), (2, 3), (3, 1)), ((2, 5), (2, 3), (3, 4)), ((2, 5), (2, 4), (3, 1)), ((2, 5), (2, 4), (3, 4)), ((2, 5), (2, 6), (3, 1)), ((2, 5), (2, 6), (3, 4)), ((2, 5), (3, 1), (1, 2)), ((2, 5), (3, 1), (2, 3)), ((2, 5), (3, 1), (2, 4)), ((2, 5), (3, 1), (2, 6)), ((2, 5), (3, 1), (3, 2)), ((2, 5), (3, 1), (3, 4)), ((2, 5), (3, 2), (3, 1)), ((2, 5), (3, 2), (3, 4)), ((2, 5), (3, 4), (1, 2)), ((2, 5), (3, 4), (2, 3)), ((2, 5), (3, 4), (2, 4)), ((2, 5), (3, 4), (2, 6)), ((2, 5), (3, 4), (3, 1)), ((2, 5), (3, 4), (3, 2)), ((2, 6), (1, 2), (3, 1)), ((2, 6), (1, 2), (3, 4)), ((2, 6), (2, 3), (3, 1)), ((2, 6), (2, 3), (3, 4)), ((2, 6), (2, 4), (3, 1)), ((2, 6), (2, 4), (3, 4)), ((2, 6), (2, 5), (3, 1)), ((2, 6), (2, 5), (3, 4)), ((2, 6), (3, 1), (1, 2)), ((2, 6), (3, 1), (2, 3)), ((2, 6), (3, 1), (2, 4)), ((2, 6), (3, 1), (2, 5)), ((2, 6), (3, 1), (3, 2)), ((2, 6), (3, 1), (3, 4)), ((2, 6), (3, 2), (3, 1)), ((2, 6), (3, 2), (3, 4)), ((2, 6), (3, 4), (1, 2)), ((2, 6), (3, 4), (2, 3)), ((2, 6), (3, 4), (2, 4)), ((2, 6), (3, 4), (2, 5)), ((2, 6), (3, 4), (3, 1)), ((2, 6), (3, 4), (3, 2)), ((3, 1), (1, 2), (2, 3)), ((3, 1), (1, 2), (2, 4)), ((3, 1), (1, 2), (2, 5)), ((3, 1), (1, 2), (2, 6)), ((3, 1), (1, 2), (3, 2)), ((3, 1), (1, 2), (3, 4)), ((3, 1), (2, 3), (1, 2)), ((3, 1), (2, 3), (2, 4)), ((3, 1), (2, 3), (2, 5)), ((3, 1), (2, 3), (2, 6)), ((3, 1), (2, 4), (1, 2)), ((3, 1), (2, 4), (2, 3)), ((3, 1), (2, 4), (2, 5)), ((3, 1), (2, 4), (2, 6)), ((3, 1), (2, 4), (3, 2)), ((3, 1), (2, 4), (3, 4)), ((3, 1), (2, 5), (1, 2)), ((3, 1), (2, 5), (2, 3)), ((3, 1), (2, 5), (2, 4)), ((3, 1), (2, 5), (2, 6)), ((3, 1), (2, 5), (3, 2)), ((3, 1), (2, 5), (3, 4)), ((3, 1), (2, 6), (1, 2)), ((3, 1), (2, 6), (2, 3)), ((3, 1), (2, 6), (2, 4)), ((3, 1), (2, 6), (2, 5)), ((3, 1), (2, 6), (3, 2)), ((3, 1), (2, 6), (3, 4)), ((3, 1), (3, 2), (1, 2)), ((3, 1), (3, 2), (2, 4)), ((3, 1), (3, 2), (2, 5)), ((3, 1), (3, 2), (2, 6)), ((3, 1), (3, 4), (1, 2)), ((3, 1), (3, 4), (2, 4)), ((3, 1), (3, 4), (2, 5)), ((3, 1), (3, 4), (2, 6)), ((3, 2), (1, 2), (3, 1)), ((3, 2), (1, 2), (3, 4)), ((3, 2), (2, 4), (3, 1)), ((3, 2), (2, 4), (3, 4)), ((3, 2), (2, 5), (3, 1)), ((3, 2), (2, 5), (3, 4)), ((3, 2), (2, 6), (3, 1)), ((3, 2), (2, 6), (3, 4)), ((3, 2), (3, 1), (1, 2)), ((3, 2), (3, 1), (2, 4)), ((3, 2), (3, 1), (2, 5)), ((3, 2), (3, 1), (2, 6)), ((3, 2), (3, 4), (1, 2)), ((3, 2), (3, 4), (2, 4)), ((3, 2), (3, 4), (2, 5)), ((3, 2), (3, 4), (2, 6)), ((3, 4), (1, 2), (2, 3)), ((3, 4), (1, 2), (2, 4)), ((3, 4), (1, 2), (2, 5)), ((3, 4), (1, 2), (2, 6)), ((3, 4), (1, 2), (3, 1)), ((3, 4), (1, 2), (3, 2)), ((3, 4), (2, 3), (1, 2)), ((3, 4), (2, 3), (2, 4)), ((3, 4), (2, 3), (2, 5)), ((3, 4), (2, 3), (2, 6)), ((3, 4), (2, 4), (1, 2)), ((3, 4), (2, 4), (2, 3)), ((3, 4), (2, 4), (2, 5)), ((3, 4), (2, 4), (2, 6)), ((3, 4), (2, 4), (3, 1)), ((3, 4), (2, 4), (3, 2)), ((3, 4), (2, 5), (1, 2)), ((3, 4), (2, 5), (2, 3)), ((3, 4), (2, 5), (2, 4)), ((3, 4), (2, 5), (2, 6)), ((3, 4), (2, 5), (3, 1)), ((3, 4), (2, 5), (3, 2)), ((3, 4), (2, 6), (1, 2)), ((3, 4), (2, 6), (2, 3)), ((3, 4), (2, 6), (2, 4)), ((3, 4), (2, 6), (2, 5)), ((3, 4), (2, 6), (3, 1)), ((3, 4), (2, 6), (3, 2)), ((3, 4), (3, 1), (1, 2)), ((3, 4), (3, 1), (2, 4)), ((3, 4), (3, 1), (2, 5)), ((3, 4), (3, 1), (2, 6)), ((3, 4), (3, 2), (1, 2)), ((3, 4), (3, 2), (2, 4)), ((3, 4), (3, 2), (2, 5)), ((3, 4), (3, 2), (2, 6))]

可以对生成器使用递归:

from collections import Counter
def groups(d, l, c = []):
  if l == len(c):
    yield c
  else:
    for i in d:
      if i not in c:
        _c = Counter([j for k in [*c, i] for j in k])
        if all(j < 3 for j in _c.values()):
           yield from groups(d, l, c+[i])

data = [(1,2),(2,3),(2,4),(2,5),(2,6),(3,1),(3,2),(3,4)]
result = list(groups(data, 3))
final_result = [a for i, a in enumerate(result) if all(any(c not in h for c in a) for h in result[:i])]

您是否可以为提供的输入提供长度2组合的示例输出>是:
[(1,2)、(2,3)]
[(1,2)、(2,4)]
[(1,2)、(2,5)]
[(1,2)、(2,6)]
[(1,2)、(3,1)],
[(1,2)、(3,2)],
[(1,2],
[(1,2)、(3,4)],
[(2,5)]
等等……那么你在这里跳过了什么?(1,2)、(2.2)?我没有
(2,2)
在我的数组中,元组中永远不会有两个相同的数字。在生成pair时,不会跳过任何内容,因为元组中的数字被使用两次的条件是真的。那么你要跳过哪些元素呢?我不知道是的,这是真的,但在我的实际应用程序中,我使用的是长度为50的数组,在这种情况下,它不会有效客户端。在这种情况下,我需要对所有置换进行快速计算,但这不是我想要的。处理大小为50的数组时,这将花费大量时间。
itertools.permutations
是一个迭代器,这意味着它在进行时进行计算,而不是一次对所有内容进行计算,因此它不是一个好的解决方案,不需要对所有排列都是生成和测试的。这将需要很多时间。不需要的对被跳过,你能在输出中告诉我哪些不需要的对没有被跳过吗?另外,在时间方面,你总是可以通过线程等并行化,我不确定你能避免生成所有的组合。此外,你可以生成所有可能的组合您需要的组合并将它们保存在某个位置,这样就不需要生成一个好的解决方案,但有一个问题。存在重复的数组,例如
[(1,2)、(2,3)、(3,1)]
[(2,3)、(1,2)、(3,1)]
。有没有办法,如何跳过它们?这是可行的,但我需要在生成组合的过程中捕获重复项,而不是在生成组合后对其进行过滤,因为在我的实际应用程序中,我使用的是大小为50的数组,在这种情况下需要花费大量时间。有没有办法在生成com时捕获组中的重复项二进制?结果还可以,但我需要生成所有的组合,而不仅仅是一个随机组合。
[[(1, 2), (2, 3), (3, 1)], [(1, 2), (2, 3), (3, 4)], [(1, 2), (2, 4), (3, 1)], [(1, 2), (2, 4), (3, 4)], [(1, 2), (2, 5), (3, 1)], [(1, 2), (2, 5), (3, 4)], [(1, 2), (2, 6), (3, 1)], [(1, 2), (2, 6), (3, 4)], [(1, 2), (3, 1), (3, 2)], [(1, 2), (3, 1), (3, 4)], [(1, 2), (3, 2), (3, 4)], [(2, 3), (2, 4), (3, 1)], [(2, 3), (2, 4), (3, 4)], [(2, 3), (2, 5), (3, 1)], [(2, 3), (2, 5), (3, 4)], [(2, 3), (2, 6), (3, 1)], [(2, 3), (2, 6), (3, 4)], [(2, 4), (2, 5), (3, 1)], [(2, 4), (2, 5), (3, 4)], [(2, 4), (2, 6), (3, 1)], [(2, 4), (2, 6), (3, 4)], [(2, 4), (3, 1), (3, 2)], [(2, 4), (3, 1), (3, 4)], [(2, 4), (3, 2), (3, 4)], [(2, 5), (2, 6), (3, 1)], [(2, 5), (2, 6), (3, 4)], [(2, 5), (3, 1), (3, 2)], [(2, 5), (3, 1), (3, 4)], [(2, 5), (3, 2), (3, 4)], [(2, 6), (3, 1), (3, 2)], [(2, 6), (3, 1), (3, 4)], [(2, 6), (3, 2), (3, 4)]]