Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.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 从一组2元组生成3元组_Python_Algorithm_Tuples - Fatal编程技术网

Python 从一组2元组生成3元组

Python 从一组2元组生成3元组,python,algorithm,tuples,Python,Algorithm,Tuples,在先前的问题中: 我从@AChampion那里得到了一个答案,如果2元组的数量可以被3整除,这个答案似乎有效。然而,如果我们有10个2元组,解决方案就会失败。在摸索了一段时间后,我的印象是不可能找到一个完美的解决方案,比如: (1,2)(1,3),(1,4),(2,3),(2,4),(3,4) 所以我想找到一个解决方案,最小化剩余元组的数量。在上述示例中,结果可能是: (1,2,3) # derived from (1,2), (1,3), (2,3) (1,4),(2,4

在先前的问题中:

我从@AChampion那里得到了一个答案,如果2元组的数量可以被3整除,这个答案似乎有效。然而,如果我们有10个2元组,解决方案就会失败。在摸索了一段时间后,我的印象是不可能找到一个完美的解决方案,比如:

(1,2)(1,3),(1,4),(2,3),(2,4),(3,4)
所以我想找到一个解决方案,最小化剩余元组的数量。在上述示例中,结果可能是:

(1,2,3)           # derived from (1,2), (1,3), (2,3)
(1,4),(2,4),(3,4) # remainder tuples 
从3 2元组生成3元组的规则是:

(a,b), (b,c), (c,a) -> (a, b, c)
i、 e.2元组是长度为3的循环。三元组中元素的顺序并不重要,即:

(a,b,c) == (c,a,b)
我对我们有一个数字n的情况很感兴趣:

for x in range(1,n+1):
    for y in range(1,n+1):
        if x!=y:
            a.append((x,y))

# a = [ (1,2),...,(1,n), (2,1),(2,3),...,(2,n),...(n,1),...,(n,n-1) ]
从a开始,最小化生成3元组时剩余的2元组数。每个2元组只能使用一次


我绞尽脑汁想了好几个小时,但我似乎无法想出一个优雅的解决方案,也没有找到一个丑陋的解决方案:-对于一般情况。有什么想法吗?

为此,您需要创建一些用于替换的组合。然后对包含上述任意组合的3个项目的数据进行循环并替换它们。 我已经分几步做了这件事

from itertools import combinations

# create replacements elements
number_combinations_raw = list(combinations(range(1, 5), 3))

# create proper number combinations
number_combinations = []
for item in number_combinations_raw:
    if (item[0] + 1 == item[1]) and (item[1] + 1 == item[2]):
        number_combinations.append(item)

# create test data
data = [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4)]

# reduce data
reduce_data = []
for number_set in number_combinations:
    count = 0
    merged_data = []
    for item in data:
        if (number_set[0] in item and number_set[1] in item) or (number_set[1] in item and number_set[2] in item) \
            or (number_set[0] in item and number_set[2] in item):
            merged_data.append(item)
            count += 1
    if count == 3:
        reduce_data.append((number_set, merged_data))

# delete merged elements from data list and add replacement
for item in data:
    for reduce_item in reduce_data:
        for element in reduce_item[1]:
            if element in data:
                data.remove(element)

        data = [reduce_item[0]] + data

# remove duplicated replaced elements
final_list = list(dict.fromkeys(data))
输出:

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

如果您包含在哪些条件下可以将3个2元组转换为3元组的信息,就会更清楚。因此,请包含链接帖子中的相关信息。2元组中的顺序是否相关?1,2,1,3,2,3与1,2,1,3,3,2相同吗?基本上,每个元组都会在那里两次正常+镜像,对吗?我添加了更多信息,希望这有助于澄清问题对不起,错过了你的最后一个问题,是的,x,y属于2元组集如果y,x属于该集,那么看看你之前的问题,你想构造一个吗?我想你的测试数据中缺少了3,4。发布后,我知道这叫做部分Steiner三重系统。在“我现在看到”中发布了一个非常好的解释,我的问题中也缺少了3,4,对此表示抱歉