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
Is this curved text blend possible in Illustrator?
Loading military units into ships optimally, using backtracking
Does a code snippet compile? Or does it get compiled?
Is there a way to unplug the Raspberry pi safely without shutting down
Write an interpreter for *
Understanding the point of a kölsche Witz
Three legged NOT gate? What is this symbol?
Ex-contractor published company source code and secrets online
Multirow in tabularx?
In a 2 layer PCB with a top layer densely populated, from an EMI & EMC point of view should the ground plane be on top, bottom or both and why?
Should you play baroque pieces a semitone lower?
Is Texas Instrument wrong with their pin number on TO-92 package?
A tool to replace all words with antonyms
Can the ground attached to neutral fool a receptacle tester?
Dropdowns & Chevrons for Right to Left languages
What is my malfunctioning AI harvesting from humans?
If "more guns less crime", how do gun advocates explain that the EU has less crime than the US?
Why do funding agencies like the NSF not publish accepted grants?
What does Apple mean by "This may decrease battery life"?
Am I overreacting to my team leader's unethical requests?
What happen to those who died but not from the snap?
How should an administrative assistant reply to student addressing them as "Professor" or "Doctor"?
I accidentally overwrote a Linux binary file
Different inverter (logic gate) symbols
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
9 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
9 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 9 hours ago
MrGeek
8,9292 gold badges14 silver badges36 bronze badges
8,9292 gold badges14 silver badges36 bronze badges
asked 9 hours ago
zachizachi
333 bronze badges
333 bronze badges
4
itertools.product
will get you half-way there.
– DeepSpace
9 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
9 hours ago
add a comment |
4
itertools.product
will get you half-way there.
– DeepSpace
9 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
9 hours ago
4
4
itertools.product
will get you half-way there.– DeepSpace
9 hours ago
itertools.product
will get you half-way there.– DeepSpace
9 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
9 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
9 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)]
Unless you are using this for sport, you should probably go with a more readable solution, e.g. one by MrGeek below.
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)]
Unless you are using this for sport, you should probably go with a more readable solution, e.g. one by MrGeek below.
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)]
Unless you are using this for sport, you should probably go with a more readable solution, e.g. one by MrGeek below.
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)]
Unless you are using this for sport, you should probably go with a more readable solution, e.g. one by MrGeek below.
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)]
Unless you are using this for sport, you should probably go with a more readable solution, e.g. one by MrGeek below.
edited 3 mins ago
answered 8 hours ago
hilberts_drinking_problemhilberts_drinking_problem
6,8793 gold badges14 silver badges32 bronze badges
6,8793 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 9 hours ago
answered 9 hours ago
MrGeekMrGeek
8,9292 gold badges14 silver badges36 bronze badges
8,9292 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 8 hours ago
answered 8 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 9 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 8 hours ago
New contributor
answered 9 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 8 hours ago
answered 8 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
9 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
9 hours ago