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

时间:2019-06-13 13:28:09

标签: python

我试图弄清楚如何编写将给定列表组合为两个列表的代码,如下面所示的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]

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

每个列表中数字的顺序无关紧要。重要的是每个列表中的值。例如,

[1,4,7,3,9]

将和

一样好
[4,3,7,1,9]

此外,交叉对,共享对和解决方案变量不一定都必须是某种类型。它们可以是列表列表,字典或元组。

感谢所有帮助和反馈。

编辑:

我已经尝试过了,而且行得通。有多个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]

2 个答案:

答案 0 :(得分: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=',')

答案 1 :(得分: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]}