David Pekker
David Pekker

Reputation: 353

list of lists with pairs that do not have a match in the original set

I have a set of tuples in a form of a list:

[(1, 2), (3, 4), (5, 6), (7, 8)]

Not necessary in such an order, it can be as well:

[(1, 4), (2, 6), (3, 5), (7, 8)]

These lists have two properties:

so, let's say I have:

a = [(1, 2), (3, 4), (5, 6), (7, 8)]

What is the way to create list of tuples b out of a such that no original tuples would be in it?

so for example

b = [(1, 8), (3, 7), (2, 6), (4, 5)]

is ok but

b = [(1, 2), (3, 7), (5, 6), (4, 8)]

is not ok, because it contains (1, 2) and (5, 6) presented in a

PS: (The title is messy, but I don't know how to explain better (will be happy to edit).

Upvotes: 1

Views: 74

Answers (3)

jpp
jpp

Reputation: 164673

Here's an idea using collections.deque and itertools. The idea is to flatten, rotate, then split into pairs. Assumes you don't require any sorting in the result.

a = [(1, 2), (3, 4), (5, 6), (7, 8)]

from itertools import chain, islice
from collections import deque

dq = deque(chain.from_iterable(a))
dq.rotate()

res = list(zip(islice(dq, 0, None, 2), islice(dq, 1, None, 2)))

print(res)

[(8, 1), (2, 3), (4, 5), (6, 7)]

Upvotes: 1

Noufal Ibrahim
Noufal Ibrahim

Reputation: 72755

Well, here's one. I might be misunderstanding your original question though.

l = [(1, 2), (3, 4), (5, 6), (7, 8)]
m = max(itertools.chain(*l)) # Largest number in input
nos = range(1, m) # List of numbers we can pick from.
all_tuples = set(itertools.combinations(nos, 2)) # All possible tuples
not_in_original = all_tuples - set(l)

Here's a test

[x in l for x in not_in_original]
[False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False]

Upvotes: 0

user2390182
user2390182

Reputation: 73460

Building on my naive approach comment:

a = [(1, 2), (3, 4), (5, 6), (7, 8)]
b = [(x[1], y[0]) for x, y in zip(a, a[1:]+a[:1])]
# [(2, 3), (4, 5), (6, 7), (8, 1)]

This pairs elements from adjacent pairs and thus guarantees no pair is in the original list

Upvotes: 7

Related Questions