create a tuple from pairsWhat is the equivalent of the C++ Pair<L,R> in Java?How can I safely create a nested directory?What's the difference between lists and tuples?What are “named tuples” in Python?Is Using .NET 4.0 Tuples in my C# Code a Poor Design Decision?How to sort (list/tuple) of lists/tuples by the element at a given index?Convert list to tuple in PythonChoose two tuples from a list and calculate every possible pair in Python
Can a fight scene, component-wise, be too complex and complicated?
ICO or PNG Format?
What happens if I delete an icloud backup?
Who are these characters/superheroes in the posters from Chris's room in Family Guy?
A simple stop watch which I want to extend
constant evaluation when using differential equations.
n-types of the theory of natural numbers?
What are the conventions for transcribing Semitic languages into Greek?
Is this curved text blend possible in Illustrator?
Is there a standardised way to check fake news?
CTCI Chapter 1 : Palindrome Permutation
Continuous vertical line using booktabs in tabularx table?
How is this kind of structure made?
Different inverter (logic gate) symbols
Is Texas Instrument wrong with their pin number on TO-92 package?
Tikzpicture - finish drawing a curved line for a cake slice
How can Radagast come across Gandalf and Thorin's company?
Generate Brainfuck for the numbers 1–255
Are differences between uniformly distributed numbers uniformly distributed?
On Math Looking Obvious in Retrospect
Can you castle with a "ghost" rook?
How should an administrative assistant reply to student addressing them as "Professor" or "Doctor"?
Should you play baroque pieces a semitone lower?
How to mark beverage cans in a cooler for a blind person?
create a tuple from pairs
What is the equivalent of the C++ Pair<L,R> in Java?How can I safely create a nested directory?What's the difference between lists and tuples?What are “named tuples” in Python?Is Using .NET 4.0 Tuples in my C# Code a Poor Design Decision?How to sort (list/tuple) of lists/tuples by the element at a given index?Convert list to tuple in PythonChoose two tuples from a list and calculate every possible pair in Python
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I would like to create a tuple which present all the possible pairs from two tuples
this is example for what I would like to receive :
first_tuple = (1, 2)
second_tuple = (4, 5)
mult_tuple(first_tuple, second_tuple)
output :
((1, 4), (4, 1), (1, 5), (5, 1), (2, 4), (4, 2), (2, 5), (5, 2))
This is what I did which succeed however look a bit cumbersome :
def mult_tuple(tuple1, tuple2):
ls=[]
for t1 in tuple1:
for t2 in tuple2:
c=(t1,t2)
d=(t2,t1)
ls.append(c)
ls.append(d)
return tuple(ls)
first_tuple = (1, 2)
second_tuple = (4, 5)
mult_tuple(first_tuple, second_tuple)
The code I wrote works , however I am looking for a nicer code
thank you in advance
python tuples
add a comment |
I would like to create a tuple which present all the possible pairs from two tuples
this is example for what I would like to receive :
first_tuple = (1, 2)
second_tuple = (4, 5)
mult_tuple(first_tuple, second_tuple)
output :
((1, 4), (4, 1), (1, 5), (5, 1), (2, 4), (4, 2), (2, 5), (5, 2))
This is what I did which succeed however look a bit cumbersome :
def mult_tuple(tuple1, tuple2):
ls=[]
for t1 in tuple1:
for t2 in tuple2:
c=(t1,t2)
d=(t2,t1)
ls.append(c)
ls.append(d)
return tuple(ls)
first_tuple = (1, 2)
second_tuple = (4, 5)
mult_tuple(first_tuple, second_tuple)
The code I wrote works , however I am looking for a nicer code
thank you in advance
python tuples
4
itertools.product
will get you half-way there.
– DeepSpace
8 hours ago
1
You might also want to consider if you’re looking for unique tuples. Would you want to add both(1, 1)
and it’s reverse?
– donkopotamus
8 hours ago
add a comment |
I would like to create a tuple which present all the possible pairs from two tuples
this is example for what I would like to receive :
first_tuple = (1, 2)
second_tuple = (4, 5)
mult_tuple(first_tuple, second_tuple)
output :
((1, 4), (4, 1), (1, 5), (5, 1), (2, 4), (4, 2), (2, 5), (5, 2))
This is what I did which succeed however look a bit cumbersome :
def mult_tuple(tuple1, tuple2):
ls=[]
for t1 in tuple1:
for t2 in tuple2:
c=(t1,t2)
d=(t2,t1)
ls.append(c)
ls.append(d)
return tuple(ls)
first_tuple = (1, 2)
second_tuple = (4, 5)
mult_tuple(first_tuple, second_tuple)
The code I wrote works , however I am looking for a nicer code
thank you in advance
python tuples
I would like to create a tuple which present all the possible pairs from two tuples
this is example for what I would like to receive :
first_tuple = (1, 2)
second_tuple = (4, 5)
mult_tuple(first_tuple, second_tuple)
output :
((1, 4), (4, 1), (1, 5), (5, 1), (2, 4), (4, 2), (2, 5), (5, 2))
This is what I did which succeed however look a bit cumbersome :
def mult_tuple(tuple1, tuple2):
ls=[]
for t1 in tuple1:
for t2 in tuple2:
c=(t1,t2)
d=(t2,t1)
ls.append(c)
ls.append(d)
return tuple(ls)
first_tuple = (1, 2)
second_tuple = (4, 5)
mult_tuple(first_tuple, second_tuple)
The code I wrote works , however I am looking for a nicer code
thank you in advance
python tuples
python tuples
edited 8 hours ago
MrGeek
8,9192 gold badges14 silver badges36 bronze badges
8,9192 gold badges14 silver badges36 bronze badges
asked 8 hours ago
zachizachi
333 bronze badges
333 bronze badges
4
itertools.product
will get you half-way there.
– DeepSpace
8 hours ago
1
You might also want to consider if you’re looking for unique tuples. Would you want to add both(1, 1)
and it’s reverse?
– donkopotamus
8 hours ago
add a comment |
4
itertools.product
will get you half-way there.
– DeepSpace
8 hours ago
1
You might also want to consider if you’re looking for unique tuples. Would you want to add both(1, 1)
and it’s reverse?
– donkopotamus
8 hours ago
4
4
itertools.product
will get you half-way there.– DeepSpace
8 hours ago
itertools.product
will get you half-way there.– DeepSpace
8 hours ago
1
1
You might also want to consider if you’re looking for unique tuples. Would you want to add both
(1, 1)
and it’s reverse?– donkopotamus
8 hours ago
You might also want to consider if you’re looking for unique tuples. Would you want to add both
(1, 1)
and it’s reverse?– donkopotamus
8 hours ago
add a comment |
6 Answers
6
active
oldest
votes
Here is an ugly one-liner.
first_tuple = (1, 2)
second_tuple = (4, 5)
tups = [first_tuple, second_tuple]
res = [(i, j) for x in tups for y in tups for i in x for j in y if x is not y]
# [(1, 4), (1, 5), (2, 4), (2, 5), (4, 1), (4, 2), (5, 1), (5, 2)]
add a comment |
You can use itertools
's product
and permutations
:
from itertools import product, permutations
ls = []
for tup in product(first_tuple, second_tuple):
ls.extend(list(permutations(tup)))
print(ls)
Output:
[(1, 4), (4, 1), (1, 5), (5, 1), (2, 4), (4, 2), (2, 5), (5, 2)]
product
produces the tuples produced equally by the double for loop structure, and permutations
produces the two permutations produced equally by your c
and d
variables, extend
is used here instead of two append
s.
add a comment |
itertools.product
gives you what you want. However, since the Cartesian product of two tuples is not commutative (product(x,y) != product(y,x)
), you need to compute both and concatenate the results.
>>> from itertools import chain, product
>>> x = (1,4)
>>> y = (2, 5)
>>> list(chain(product(x,y), product(y,x)))
[(1, 2), (1, 5), (4, 2), (4, 5), (2, 1), (2, 4), (5, 1), (5, 4)]
(You can use chain
here instead of permutations
because there are only two permutations of a 2-tuple, which are easy enough to specify explicitly.)
add a comment |
If you’d like to avoid the use of the standard library (itertools
) then simply combine two list comprehensions:
result = [(x, y) for x in first_tuple for y in second_tuple]
result.extend( (x, y) for x in second_tuple for y in first_tuple )
then convert to a tuple
if it’s important to you.
add a comment |
Also You can do:
from itertools import permutations
t1=(1,2)
t2=(3,4)
my_tuple=tuple([key for key in filter(lambda x: x!=t1 and (x!=t2),list(permutations(t1+t2,2)))])
New contributor
add a comment |
first_tuple = (1, 2)
second_tuple = (4, 5)
out = []
for val in first_tuple:
for val2 in second_tuple:
out.append((val, val2))
out.append((val2, val))
print(tuple(out))
Prints:
((1, 4), (4, 1), (1, 5), (5, 1), (2, 4), (4, 2), (2, 5), (5, 2))
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f57453407%2fcreate-a-tuple-from-pairs%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
Here is an ugly one-liner.
first_tuple = (1, 2)
second_tuple = (4, 5)
tups = [first_tuple, second_tuple]
res = [(i, j) for x in tups for y in tups for i in x for j in y if x is not y]
# [(1, 4), (1, 5), (2, 4), (2, 5), (4, 1), (4, 2), (5, 1), (5, 2)]
add a comment |
Here is an ugly one-liner.
first_tuple = (1, 2)
second_tuple = (4, 5)
tups = [first_tuple, second_tuple]
res = [(i, j) for x in tups for y in tups for i in x for j in y if x is not y]
# [(1, 4), (1, 5), (2, 4), (2, 5), (4, 1), (4, 2), (5, 1), (5, 2)]
add a comment |
Here is an ugly one-liner.
first_tuple = (1, 2)
second_tuple = (4, 5)
tups = [first_tuple, second_tuple]
res = [(i, j) for x in tups for y in tups for i in x for j in y if x is not y]
# [(1, 4), (1, 5), (2, 4), (2, 5), (4, 1), (4, 2), (5, 1), (5, 2)]
Here is an ugly one-liner.
first_tuple = (1, 2)
second_tuple = (4, 5)
tups = [first_tuple, second_tuple]
res = [(i, j) for x in tups for y in tups for i in x for j in y if x is not y]
# [(1, 4), (1, 5), (2, 4), (2, 5), (4, 1), (4, 2), (5, 1), (5, 2)]
answered 7 hours ago
hilberts_drinking_problemhilberts_drinking_problem
6,8243 gold badges14 silver badges32 bronze badges
6,8243 gold badges14 silver badges32 bronze badges
add a comment |
add a comment |
You can use itertools
's product
and permutations
:
from itertools import product, permutations
ls = []
for tup in product(first_tuple, second_tuple):
ls.extend(list(permutations(tup)))
print(ls)
Output:
[(1, 4), (4, 1), (1, 5), (5, 1), (2, 4), (4, 2), (2, 5), (5, 2)]
product
produces the tuples produced equally by the double for loop structure, and permutations
produces the two permutations produced equally by your c
and d
variables, extend
is used here instead of two append
s.
add a comment |
You can use itertools
's product
and permutations
:
from itertools import product, permutations
ls = []
for tup in product(first_tuple, second_tuple):
ls.extend(list(permutations(tup)))
print(ls)
Output:
[(1, 4), (4, 1), (1, 5), (5, 1), (2, 4), (4, 2), (2, 5), (5, 2)]
product
produces the tuples produced equally by the double for loop structure, and permutations
produces the two permutations produced equally by your c
and d
variables, extend
is used here instead of two append
s.
add a comment |
You can use itertools
's product
and permutations
:
from itertools import product, permutations
ls = []
for tup in product(first_tuple, second_tuple):
ls.extend(list(permutations(tup)))
print(ls)
Output:
[(1, 4), (4, 1), (1, 5), (5, 1), (2, 4), (4, 2), (2, 5), (5, 2)]
product
produces the tuples produced equally by the double for loop structure, and permutations
produces the two permutations produced equally by your c
and d
variables, extend
is used here instead of two append
s.
You can use itertools
's product
and permutations
:
from itertools import product, permutations
ls = []
for tup in product(first_tuple, second_tuple):
ls.extend(list(permutations(tup)))
print(ls)
Output:
[(1, 4), (4, 1), (1, 5), (5, 1), (2, 4), (4, 2), (2, 5), (5, 2)]
product
produces the tuples produced equally by the double for loop structure, and permutations
produces the two permutations produced equally by your c
and d
variables, extend
is used here instead of two append
s.
edited 7 hours ago
answered 8 hours ago
MrGeekMrGeek
8,9192 gold badges14 silver badges36 bronze badges
8,9192 gold badges14 silver badges36 bronze badges
add a comment |
add a comment |
itertools.product
gives you what you want. However, since the Cartesian product of two tuples is not commutative (product(x,y) != product(y,x)
), you need to compute both and concatenate the results.
>>> from itertools import chain, product
>>> x = (1,4)
>>> y = (2, 5)
>>> list(chain(product(x,y), product(y,x)))
[(1, 2), (1, 5), (4, 2), (4, 5), (2, 1), (2, 4), (5, 1), (5, 4)]
(You can use chain
here instead of permutations
because there are only two permutations of a 2-tuple, which are easy enough to specify explicitly.)
add a comment |
itertools.product
gives you what you want. However, since the Cartesian product of two tuples is not commutative (product(x,y) != product(y,x)
), you need to compute both and concatenate the results.
>>> from itertools import chain, product
>>> x = (1,4)
>>> y = (2, 5)
>>> list(chain(product(x,y), product(y,x)))
[(1, 2), (1, 5), (4, 2), (4, 5), (2, 1), (2, 4), (5, 1), (5, 4)]
(You can use chain
here instead of permutations
because there are only two permutations of a 2-tuple, which are easy enough to specify explicitly.)
add a comment |
itertools.product
gives you what you want. However, since the Cartesian product of two tuples is not commutative (product(x,y) != product(y,x)
), you need to compute both and concatenate the results.
>>> from itertools import chain, product
>>> x = (1,4)
>>> y = (2, 5)
>>> list(chain(product(x,y), product(y,x)))
[(1, 2), (1, 5), (4, 2), (4, 5), (2, 1), (2, 4), (5, 1), (5, 4)]
(You can use chain
here instead of permutations
because there are only two permutations of a 2-tuple, which are easy enough to specify explicitly.)
itertools.product
gives you what you want. However, since the Cartesian product of two tuples is not commutative (product(x,y) != product(y,x)
), you need to compute both and concatenate the results.
>>> from itertools import chain, product
>>> x = (1,4)
>>> y = (2, 5)
>>> list(chain(product(x,y), product(y,x)))
[(1, 2), (1, 5), (4, 2), (4, 5), (2, 1), (2, 4), (5, 1), (5, 4)]
(You can use chain
here instead of permutations
because there are only two permutations of a 2-tuple, which are easy enough to specify explicitly.)
edited 7 hours ago
answered 7 hours ago
chepnerchepner
282k40 gold badges277 silver badges375 bronze badges
282k40 gold badges277 silver badges375 bronze badges
add a comment |
add a comment |
If you’d like to avoid the use of the standard library (itertools
) then simply combine two list comprehensions:
result = [(x, y) for x in first_tuple for y in second_tuple]
result.extend( (x, y) for x in second_tuple for y in first_tuple )
then convert to a tuple
if it’s important to you.
add a comment |
If you’d like to avoid the use of the standard library (itertools
) then simply combine two list comprehensions:
result = [(x, y) for x in first_tuple for y in second_tuple]
result.extend( (x, y) for x in second_tuple for y in first_tuple )
then convert to a tuple
if it’s important to you.
add a comment |
If you’d like to avoid the use of the standard library (itertools
) then simply combine two list comprehensions:
result = [(x, y) for x in first_tuple for y in second_tuple]
result.extend( (x, y) for x in second_tuple for y in first_tuple )
then convert to a tuple
if it’s important to you.
If you’d like to avoid the use of the standard library (itertools
) then simply combine two list comprehensions:
result = [(x, y) for x in first_tuple for y in second_tuple]
result.extend( (x, y) for x in second_tuple for y in first_tuple )
then convert to a tuple
if it’s important to you.
answered 8 hours ago
donkopotamusdonkopotamus
13.7k1 gold badge27 silver badges41 bronze badges
13.7k1 gold badge27 silver badges41 bronze badges
add a comment |
add a comment |
Also You can do:
from itertools import permutations
t1=(1,2)
t2=(3,4)
my_tuple=tuple([key for key in filter(lambda x: x!=t1 and (x!=t2),list(permutations(t1+t2,2)))])
New contributor
add a comment |
Also You can do:
from itertools import permutations
t1=(1,2)
t2=(3,4)
my_tuple=tuple([key for key in filter(lambda x: x!=t1 and (x!=t2),list(permutations(t1+t2,2)))])
New contributor
add a comment |
Also You can do:
from itertools import permutations
t1=(1,2)
t2=(3,4)
my_tuple=tuple([key for key in filter(lambda x: x!=t1 and (x!=t2),list(permutations(t1+t2,2)))])
New contributor
Also You can do:
from itertools import permutations
t1=(1,2)
t2=(3,4)
my_tuple=tuple([key for key in filter(lambda x: x!=t1 and (x!=t2),list(permutations(t1+t2,2)))])
New contributor
edited 6 hours ago
New contributor
answered 7 hours ago
lostCodelostCode
1366 bronze badges
1366 bronze badges
New contributor
New contributor
add a comment |
add a comment |
first_tuple = (1, 2)
second_tuple = (4, 5)
out = []
for val in first_tuple:
for val2 in second_tuple:
out.append((val, val2))
out.append((val2, val))
print(tuple(out))
Prints:
((1, 4), (4, 1), (1, 5), (5, 1), (2, 4), (4, 2), (2, 5), (5, 2))
add a comment |
first_tuple = (1, 2)
second_tuple = (4, 5)
out = []
for val in first_tuple:
for val2 in second_tuple:
out.append((val, val2))
out.append((val2, val))
print(tuple(out))
Prints:
((1, 4), (4, 1), (1, 5), (5, 1), (2, 4), (4, 2), (2, 5), (5, 2))
add a comment |
first_tuple = (1, 2)
second_tuple = (4, 5)
out = []
for val in first_tuple:
for val2 in second_tuple:
out.append((val, val2))
out.append((val2, val))
print(tuple(out))
Prints:
((1, 4), (4, 1), (1, 5), (5, 1), (2, 4), (4, 2), (2, 5), (5, 2))
first_tuple = (1, 2)
second_tuple = (4, 5)
out = []
for val in first_tuple:
for val2 in second_tuple:
out.append((val, val2))
out.append((val2, val))
print(tuple(out))
Prints:
((1, 4), (4, 1), (1, 5), (5, 1), (2, 4), (4, 2), (2, 5), (5, 2))
edited 6 hours ago
answered 7 hours ago
Andrej KeselyAndrej Kesely
18k2 gold badges10 silver badges34 bronze badges
18k2 gold badges10 silver badges34 bronze badges
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f57453407%2fcreate-a-tuple-from-pairs%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
4
itertools.product
will get you half-way there.– DeepSpace
8 hours ago
1
You might also want to consider if you’re looking for unique tuples. Would you want to add both
(1, 1)
and it’s reverse?– donkopotamus
8 hours ago