Reputation: 353
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:
n
(like [1, 2, 3,..n]
)n
is evenn
is larger than 2
all values are unique (no value is repeated).
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
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
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
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