如何将列表组合以创建两个特定的列表集合

3

我正在尝试弄清楚如何编写代码,将给定的列表组合成两个列表,如下所示的Solution变量。基本上,哪段代码可以让我从

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]

to

Solution = [1,4,7,3,9], [6,2,10,5,8]

每个列表中的数字顺序并不重要,重要的是每个列表中的值。例如,
[1,4,7,3,9]

和......一样好

[4,3,7,1,9]

此外,Crosspairs、Sharedpairs和Solution变量不一定都是某种类型。它们可以是列表的列表、字典或元组。
感谢所有帮助和反馈。
编辑:
我尝试过这个方法,它可以工作。虽然有多个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])

for i, p in enumerate(Sharedpairs):
    print(i,p)
    if (p[0] in Group1):
        Group1.append(p[1])
        Sharedpairs.remove(p)
        print('Group 1', Group1)
for i, p in enumerate(Sharedpairs):
    print(i,p)
    if (p[1] in Group1):
        Group1.append(p[0])
        Sharedpairs.remove(p)
        print('Group 1', Group1)
for i, p in enumerate(Sharedpairs):
    print(i,p)
    if (p[0] in Group1):
        Group1.append(p[1])
        Sharedpairs.remove(p)
        print('Group 1', Group1)
    elif (p[1] in Group1):
        Group1.append(p[0])
        Sharedpairs.remove(p)
        print('Group 1', Group1)
    else:
        print('Not in group')
        continue
Group1
[1, 4, 7, 3, 9]


1
不确定为什么数字 7 和 10 会交换? - yatu
1
你能解释一下第二个sharedpair的输出逻辑吗?第一个是交替获取最大值和最小值。 - sahasrara62
@yatu,7和10列表的顺序无关紧要。 - bpozo27
提示:想象一下还没有分配给side1或side2的较小集合。想象在找到连接时将它们合并,使它们像水滴合并成两个大水坑一样增长。 - Kenny Ostrom
1
@Zhenhir 为了编写方便,是的,列表项会在值中,而键可能是行号或行名称。 - bpozo27
显示剩余15条评论
2个回答

0

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

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=',')

0

这假设Sharedpairs的顺序将始终说明来自第1行的成对项,然后是来自第2行的成对项。您可以通过这种方式从任一列表中获取解决方案,并验证解决方案是否匹配。您确实需要Crosspairs来确定行数..否则我想您需要手动输入。

我使用了集合,因为您说顺序不重要..这使得处理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})

编辑:将此交易与 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}, {1, 2, 3, 4, 5}, {6, 7, 8, 9, 10})

现在也可以使用字典了.. 使用以下内容进行测试:

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]}

1
如果我们假设Crosspairs是zip,那么您可能想要使用zip(* Crosspairs)。 - Kenny Ostrom
还是得加入一些丑陋的代码才能得到一个集合元组,但是这个想法很好..谢谢。 - Zhenhir

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接