Python 如何组合列表以创建两组特定的列表

Python 如何组合列表以创建两组特定的列表,python,Python,我试图找出如何编写一个代码,将给定的列表组合成两个列表,如下面所示的解决方案变量中所指定的。基本上,是什么代码让我从 Crosspairs=[1,6]、[4,2]、[7,10]、[3,5]、[9,8] Sharedpairs=[1,4]、[3,9]、[4,7]、[3,7]、[2,6]、[2,10]、[5,8]、[10,5] 到 每个列表中数字的顺序并不重要。重要的是每个列表中的值。比如说, [1,4,7,3,9] 就跟 [4,3,7,1,9] 此外,交叉对、SharedPair和解决方案变

我试图找出如何编写一个代码,将给定的列表组合成两个列表,如下面所示的解决方案变量中所指定的。基本上,是什么代码让我从

Crosspairs=[1,6]、[4,2]、[7,10]、[3,5]、[9,8]
Sharedpairs=[1,4]、[3,9]、[4,7]、[3,7]、[2,6]、[2,10]、[5,8]、[10,5]

每个列表中数字的顺序并不重要。重要的是每个列表中的值。比如说,

[1,4,7,3,9]
就跟

[4,3,7,1,9]
此外,交叉对、SharedPair和解决方案变量并不都必须是特定类型。它们可以是列表列表、字典或元组

我感谢所有的帮助和反馈

编辑:

我试过这个,效果很好。有多个for循环,但除非提出更好的方法,否则现在就可以了

Sharedpairs=[[1,4]、[3,9]、[4,7]、[3,7]、[2,6]、[2,10]、[5,8]、[10,5]]
Group1=Sharedpairs[0]
Sharedpairs.remove(Sharedpairs[0])
对于枚举中的i,p(Sharedpairs):
打印(i,p)
如果(第1组中的p[0]:
Group1.append(p[1])
Sharedpairs.remove(p)
打印('组1',组1)
对于枚举中的i,p(Sharedpairs):
打印(i,p)
如果(第1组中的p[1]):
Group1.append(p[0])
Sharedpairs.remove(p)
打印('组1',组1)
对于枚举中的i,p(Sharedpairs):
打印(i,p)
如果(第1组中的p[0]:
Group1.append(p[1])
Sharedpairs.remove(p)
打印('组1',组1)
elif(第1组中的p[1]:
Group1.append(p[0])
Sharedpairs.remove(p)
打印('组1',组1)
其他:
打印('不在组中')
持续
第一组
[1, 4, 7, 3, 9]

这不是最佳解决方案,但它可以检查创建的行是否正确

Crosspairs = [[1,6], [4,2], [7,10], [3,5], [9,8]]
Sharedpairs = [[1,4], [3,9], [4,7], [3,7], [2,6], [2,10], [5,8], [10,5]]
# taking rows
l1= [i[0] for i in Crosspairs]
l2=[i[1] for i in Crosspairs]

# creating shared pair
match = [ sorted([l1[i],l1[i+1]])for i in range(len(l1)-1)] + [ sorted([l2[i],l2[i+1]])for i in range(len(l2)-1)] 

# sorting to match whether the created shared pair equal to orignal share pair
# if yes then print the two rows

match.sort(key=lambda x:[x[0],x[1]])

sp2 = [sorted(i) for i in Sharedpairs]
sp2.sort(key=lambda x:[x[0],x[1]])


if sp2 == match:
    print(l1,l2,sep=',')

这假设
Sharedpairs
的顺序总是先显示第1行的成对,然后显示第2行的成对。。您可以通过这种方式从任一列表中获取解决方案,并验证解决方案是否匹配。您确实需要
交叉对
来确定行数。。否则我想你需要手动输入

既然你说顺序无关紧要,我就冒昧地使用了电视机。。这使得在
Sharedpairs
中处理重复项变得更容易

#!/usr/bin/env python3
from collections import defaultdict

Crosspairs = [1,6], [4,2], [7,10], [3,5], [9,8]
Sharedpairs = [1,4], [3,9], [4,7], [3,7], [2,6], [2,10], [5,8], [10,5]
OP_Solution = set([1,4,7,3,9]), set([6,2,10,5,8])

# Allow for dicts (tested).. this assumes ordering the keys will give us
# The rows/columns in the same order as your inputs..
# I think anything fancier than that will require a specific example
# Of a dict input
def convert_dict(x):
    if isinstance(x, dict):
        return [v for k,v in sorted(x.items())]
    return x

Crosspairs = convert_dict(Crosspairs)
Sharedpairs = convert_dict(Sharedpairs)
"""
Representations of data
1, 4, 7,  3, 9
6, 2, 10, 5, 8

1, 6
4, 2
7, 10
3, 5
9, 8
"""

# Get number of rows from Crosspairs
n_rows = len(Crosspairs[0])

# This is all you need to get the result from Crosspairs
Cross_Solution = tuple(set(x) for x in zip(*Crosspairs))

# Create a defaultdict so we can have multiple sets depending
# On what row we're up to.
Shared_Solution = defaultdict(set)
row_n = 0
i = 0
for row in Sharedpairs:
    i += 1
    # If our counter (i) is greater than all our pairs
    # Divided by the number of rows we're up to a new row
    if i > len(Sharedpairs) / n_rows:
        row_n += 1
        i = 1

    # Row 1 will be under Shared_Solution[0]
    for x in row:
        Shared_Solution[row_n].add(x)

Shared_Solution = tuple(Shared_Solution.values())

# All our solutions are the same.. print one of them.. doesn't matter which.
print(OP_Solution == Cross_Solution == Shared_Solution)
print(Cross_Solution)
结果:

True
({1, 3, 4, 7, 9}, {2, 5, 6, 8, 10})
True
({1, 3, 4, 7, 9}, {2, 5, 6, 8, 10}, {1, 2, 3, 4, 5}, {6, 7, 8, 9, 10})
编辑:对
n
行进行此处理。。而不仅仅是2

如果我将事情更改为:

Crosspairs = [1,6,1,6], [4,2,2,7], [7,10,3,8], [3,5,4,9], [9,8,5,10]
Sharedpairs = [1,4], [3,9], [4,7], [3,7], [2,6], [2,10], [5,8], [10,5],\
        [1,2], [2,3], [3,4], [4,5], [6,7], [7,8], [8,9], [9,10]
OP_Solution = set([1,4,7,3,9]), set([6,2,10,5,8]), set([1,2,3,4,5]), set([6,7,8,9,10])
结果:

True
({1, 3, 4, 7, 9}, {2, 5, 6, 8, 10})
True
({1, 3, 4, 7, 9}, {2, 5, 6, 8, 10}, {1, 2, 3, 4, 5}, {6, 7, 8, 9, 10})
现在也适用于dicts。。用这个测试:

Crosspairs = {"col1":[1,6,1,6], "col2":[4,2,2,7], "col3":[7,10,3,8], "col4":[3,5,4,9], "col5":[9,8,5,10]}

不知道为什么7和10是互换的?也许你可以发布你的尝试。你能解释一下逻辑吗,第二个sharedpair的输出是如何工作的,第一个是获取最大值,最小值alternate@yatu7和10列表的顺序无关紧要。@Zhenhir为了便于编码,是的,列表项将位于值中,键可能是行号或行名。如果我们假设交叉对是zip,那么您可能想要使用zip(*交叉对),但仍然需要添加一些丑陋的元素才能获得集合的元组,但这是个好主意。。非常感谢。