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]