How to combine lists to create two specific sets of lists

Viewed 69

I'm trying to figure how to write a code that combines given lists into a two lists as specified in the Solution variable shown below. Basically, what is the code that will make me go from

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]

The order of the numbers in each list does not matter. What matters are the values in each list. For example,

[1,4,7,3,9]

would be just as good as

[4,3,7,1,9]

Also, the Crosspairs, Sharedpairs, and Solution variables do not all have to be a certain type. They can be a list of lists, a dictionary, or a tuple.

I appreciate all the help and feedback.

EDIT:

I've tried this and it works. There's multiple for loops but it'll do for now unless something better is brought up.

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 Answers

this is not optimum solution, but it can check if the created rows are correct or not

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

This assumes the order of Sharedpairs will always illustrate pairs from row 1 and then pairs from row 2.. You can get the solution from either list this way and just verify that the solutions match. You do need Crosspairs to determine the number of rows.. otherwise I guess you'd need to put that in manually.

I took the liberty of using sets since you said order doesn't matter.. and that makes dealing with duplicates in Sharedpairs easier.

#!/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)

Result:

True
({1, 3, 4, 7, 9}, {2, 5, 6, 8, 10})

EDIT: Made this deal with n rows.. instead of just 2.

If I change things to:

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

Result:

True
({1, 3, 4, 7, 9}, {2, 5, 6, 8, 10}, {1, 2, 3, 4, 5}, {6, 7, 8, 9, 10})

Also works with dicts now.. tested with this:

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