Python, list of pairs of elements from other lists Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!How do I check if a list is empty?What are metaclasses in Python?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonDoes Python have a ternary conditional operator?Getting the last element of a list in PythonHow to make a flat list out of list of listsHow do I get the number of elements in a list in Python?How do I remove a particular element from an array in JavaScript?Why not inherit from List<T>?
Did pre-Columbian Americans know the spherical shape of the Earth?
Why not send Voyager 3 and 4 following up the paths taken by Voyager 1 and 2 to re-transmit signals of later as they fly away from Earth?
Can an iPhone 7 be made to function as a NFC Tag?
AppleTVs create a chatty alternate WiFi network
Nose gear failure in single prop aircraft: belly landing or nose-gear up landing?
Is it dangerous to install hacking tools on my private linux machine?
One-one communication
Delete free apps from library
Did any compiler fully use 80-bit floating point?
I got rid of Mac OSX and replaced it with linux but now I can't change it back to OSX or windows
Moving a wrapfig vertically to encroach partially on a subsection title
Co-worker has annoying ringtone
How to change the tick of the color bar legend to black
Can you force honesty by using the Speak with Dead and Zone of Truth spells together?
Simple Http Server
Monty Hall Problem-Probability Paradox
Tannaka duality for semisimple groups
Putting class ranking in CV, but against dept guidelines
What are the main differences between the original Stargate SG-1 and the Final Cut edition?
Why datecode is SO IMPORTANT to chip manufacturers?
Asymptotics question
Why are vacuum tubes still used in amateur radios?
Project Euler #1 in C++
Should a wizard buy fine inks every time he want to copy spells into his spellbook?
Python, list of pairs of elements from other lists
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!How do I check if a list is empty?What are metaclasses in Python?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonDoes Python have a ternary conditional operator?Getting the last element of a list in PythonHow to make a flat list out of list of listsHow do I get the number of elements in a list in Python?How do I remove a particular element from an array in JavaScript?Why not inherit from List<T>?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have a series of lists, call them A
, B
, C
, D
, E
. Now every list has 5 elements with identical names, say:
A: [ 'Cars_A', 'Planes_A', 'Houses_A', 'Bikes_A' ]
B: [ 'Cars_B', 'Planes_B', 'Houses_B', 'Bikes_B' ]
etc..
What I want is a list of lists, of the form:
[ ['Cars_A', 'Planes_B'], ['Cars_A', 'Houses_B'], ['Cars_A', 'Bikes_B'],
['Planes_A', 'Cars_B'], ['Planes_A', 'Houses_B'], ['Planes_A', 'Bikes_B'],
['Houses_A', 'Cars_B'], ['Houses_A', 'Planes_B'], ['Houses_A', 'Bikes_B'],
['Bikes_A', 'Cars_B'], ['Bikes_A', 'Planes_B'], ['Bikes_A', 'Houses_B'] ]
As can be seen, the rule for this list is:
- An element cannot be grouped with another element from the same set, for example
['Cars_A', 'Planes_A']
is not allowed. - An element cannot be grouped with a similar element from a different set, for example
['Cars_A', 'Cars_B']
is not allowed.
My attempt right now is to do nested for loops for all 5 lists, but I want to avoid this if possible. Any ideas?
python arrays python-3.x list
|
show 1 more comment
I have a series of lists, call them A
, B
, C
, D
, E
. Now every list has 5 elements with identical names, say:
A: [ 'Cars_A', 'Planes_A', 'Houses_A', 'Bikes_A' ]
B: [ 'Cars_B', 'Planes_B', 'Houses_B', 'Bikes_B' ]
etc..
What I want is a list of lists, of the form:
[ ['Cars_A', 'Planes_B'], ['Cars_A', 'Houses_B'], ['Cars_A', 'Bikes_B'],
['Planes_A', 'Cars_B'], ['Planes_A', 'Houses_B'], ['Planes_A', 'Bikes_B'],
['Houses_A', 'Cars_B'], ['Houses_A', 'Planes_B'], ['Houses_A', 'Bikes_B'],
['Bikes_A', 'Cars_B'], ['Bikes_A', 'Planes_B'], ['Bikes_A', 'Houses_B'] ]
As can be seen, the rule for this list is:
- An element cannot be grouped with another element from the same set, for example
['Cars_A', 'Planes_A']
is not allowed. - An element cannot be grouped with a similar element from a different set, for example
['Cars_A', 'Cars_B']
is not allowed.
My attempt right now is to do nested for loops for all 5 lists, but I want to avoid this if possible. Any ideas?
python arrays python-3.x list
changed, they are all lists, sorry about that.
– Qubix
Mar 22 at 11:51
You said My attempt right now is to do nested for loops for all 5 lists. Can you show us?
– Yusufsn
Mar 22 at 11:52
How do you define "similar" element? Is it "same text up to the underscore" or sth else?
– Ilia Gilmijarow
Mar 22 at 11:57
Same text up to underscore, yes.
– Qubix
Mar 22 at 12:00
You say the order inside lists is the same, but are all elements also the same (apart from obvius "_A", "_B" difference)? the example suggests so.
– Ilia Gilmijarow
Mar 22 at 12:00
|
show 1 more comment
I have a series of lists, call them A
, B
, C
, D
, E
. Now every list has 5 elements with identical names, say:
A: [ 'Cars_A', 'Planes_A', 'Houses_A', 'Bikes_A' ]
B: [ 'Cars_B', 'Planes_B', 'Houses_B', 'Bikes_B' ]
etc..
What I want is a list of lists, of the form:
[ ['Cars_A', 'Planes_B'], ['Cars_A', 'Houses_B'], ['Cars_A', 'Bikes_B'],
['Planes_A', 'Cars_B'], ['Planes_A', 'Houses_B'], ['Planes_A', 'Bikes_B'],
['Houses_A', 'Cars_B'], ['Houses_A', 'Planes_B'], ['Houses_A', 'Bikes_B'],
['Bikes_A', 'Cars_B'], ['Bikes_A', 'Planes_B'], ['Bikes_A', 'Houses_B'] ]
As can be seen, the rule for this list is:
- An element cannot be grouped with another element from the same set, for example
['Cars_A', 'Planes_A']
is not allowed. - An element cannot be grouped with a similar element from a different set, for example
['Cars_A', 'Cars_B']
is not allowed.
My attempt right now is to do nested for loops for all 5 lists, but I want to avoid this if possible. Any ideas?
python arrays python-3.x list
I have a series of lists, call them A
, B
, C
, D
, E
. Now every list has 5 elements with identical names, say:
A: [ 'Cars_A', 'Planes_A', 'Houses_A', 'Bikes_A' ]
B: [ 'Cars_B', 'Planes_B', 'Houses_B', 'Bikes_B' ]
etc..
What I want is a list of lists, of the form:
[ ['Cars_A', 'Planes_B'], ['Cars_A', 'Houses_B'], ['Cars_A', 'Bikes_B'],
['Planes_A', 'Cars_B'], ['Planes_A', 'Houses_B'], ['Planes_A', 'Bikes_B'],
['Houses_A', 'Cars_B'], ['Houses_A', 'Planes_B'], ['Houses_A', 'Bikes_B'],
['Bikes_A', 'Cars_B'], ['Bikes_A', 'Planes_B'], ['Bikes_A', 'Houses_B'] ]
As can be seen, the rule for this list is:
- An element cannot be grouped with another element from the same set, for example
['Cars_A', 'Planes_A']
is not allowed. - An element cannot be grouped with a similar element from a different set, for example
['Cars_A', 'Cars_B']
is not allowed.
My attempt right now is to do nested for loops for all 5 lists, but I want to avoid this if possible. Any ideas?
python arrays python-3.x list
python arrays python-3.x list
edited Mar 22 at 11:57
pistol2myhead
2,02211027
2,02211027
asked Mar 22 at 11:49
QubixQubix
82931328
82931328
changed, they are all lists, sorry about that.
– Qubix
Mar 22 at 11:51
You said My attempt right now is to do nested for loops for all 5 lists. Can you show us?
– Yusufsn
Mar 22 at 11:52
How do you define "similar" element? Is it "same text up to the underscore" or sth else?
– Ilia Gilmijarow
Mar 22 at 11:57
Same text up to underscore, yes.
– Qubix
Mar 22 at 12:00
You say the order inside lists is the same, but are all elements also the same (apart from obvius "_A", "_B" difference)? the example suggests so.
– Ilia Gilmijarow
Mar 22 at 12:00
|
show 1 more comment
changed, they are all lists, sorry about that.
– Qubix
Mar 22 at 11:51
You said My attempt right now is to do nested for loops for all 5 lists. Can you show us?
– Yusufsn
Mar 22 at 11:52
How do you define "similar" element? Is it "same text up to the underscore" or sth else?
– Ilia Gilmijarow
Mar 22 at 11:57
Same text up to underscore, yes.
– Qubix
Mar 22 at 12:00
You say the order inside lists is the same, but are all elements also the same (apart from obvius "_A", "_B" difference)? the example suggests so.
– Ilia Gilmijarow
Mar 22 at 12:00
changed, they are all lists, sorry about that.
– Qubix
Mar 22 at 11:51
changed, they are all lists, sorry about that.
– Qubix
Mar 22 at 11:51
You said My attempt right now is to do nested for loops for all 5 lists. Can you show us?
– Yusufsn
Mar 22 at 11:52
You said My attempt right now is to do nested for loops for all 5 lists. Can you show us?
– Yusufsn
Mar 22 at 11:52
How do you define "similar" element? Is it "same text up to the underscore" or sth else?
– Ilia Gilmijarow
Mar 22 at 11:57
How do you define "similar" element? Is it "same text up to the underscore" or sth else?
– Ilia Gilmijarow
Mar 22 at 11:57
Same text up to underscore, yes.
– Qubix
Mar 22 at 12:00
Same text up to underscore, yes.
– Qubix
Mar 22 at 12:00
You say the order inside lists is the same, but are all elements also the same (apart from obvius "_A", "_B" difference)? the example suggests so.
– Ilia Gilmijarow
Mar 22 at 12:00
You say the order inside lists is the same, but are all elements also the same (apart from obvius "_A", "_B" difference)? the example suggests so.
– Ilia Gilmijarow
Mar 22 at 12:00
|
show 1 more comment
3 Answers
3
active
oldest
votes
Using itertools.permutations
and itertools.product
with filter
:
import itertools
l = [['_'.join([i,g])for i in ['cars', 'planes', 'houses', 'bikes']] for g in 'ABCDE']
l
[['cars_A', 'planes_A', 'houses_A', 'bikes_A'],
['cars_B', 'planes_B', 'houses_B', 'bikes_B'],
['cars_C', 'planes_C', 'houses_C', 'bikes_C'],
['cars_D', 'planes_D', 'houses_D', 'bikes_D'],
['cars_E', 'planes_E', 'houses_E', 'bikes_E']]
res = []
for sub in itertools.permutations(l, 2):
res.extend(list(filter(lambda x: (x[0].split('_')[0]!=x[1].split('_')[0]), itertools.product(*sub))))
res
[('cars_A', 'planes_B'),
('cars_A', 'houses_B'),
('cars_A', 'bikes_B'),
('planes_A', 'cars_B'),
('planes_A', 'houses_B'),
('planes_A', 'bikes_B'),
('houses_A', 'cars_B'),
...
('bikes_E', 'cars_D'),
('bikes_E', 'planes_D'),
('bikes_E', 'houses_D')]
thatlambda
line is quite unpythonic, trying to squeeze a bunch of logic in to one line
– aws_apprentice
Mar 22 at 12:10
using permutations is nice one
– soheshdoshi
Mar 22 at 12:18
add a comment |
Here is a simple way using itertools.combinations
, let's just make all the pairs first and then filter
afterwards.
from itertools import combinations
def filter_(tup):
x, y = tup
p1 = x.split('_')
p2 = y.split('_')
return (p1[0] != p2[0]) and (p1[1] != p2[1])
list(filter(filter_, combinations([*A, *B], 2)))
[('Cars_A', 'Planes_B'),
('Cars_A', 'Houses_B'),
('Cars_A', 'Bikes_B'),
('Planes_A', 'Cars_B'),
('Planes_A', 'Houses_B'),
('Planes_A', 'Bikes_B'),
('Houses_A', 'Cars_B'),
('Houses_A', 'Planes_B'),
('Houses_A', 'Bikes_B'),
('Bikes_A', 'Cars_B'),
('Bikes_A', 'Planes_B'),
('Bikes_A', 'Houses_B')]
list(filter(filter_, combinations([*A, *B, *C], 2)))
[('Cars_A', 'Planes_B'),
('Cars_A', 'Houses_B'),
('Cars_A', 'Bikes_B'),
('Cars_A', 'Planes_C'),
('Cars_A', 'Houses_C'),
('Cars_A', 'Bikes_C'),
('Planes_A', 'Cars_B'),
('Planes_A', 'Houses_B'),
('Planes_A', 'Bikes_B'),
('Planes_A', 'Cars_C'),
('Planes_A', 'Houses_C'),
('Planes_A', 'Bikes_C'),
('Houses_A', 'Cars_B'),
...
add a comment |
here is a way to do it without itertools
, but using a fast data stucture from collections
module, named deque
from collections import deque
A=[ 'Cars_A', 'Planes_A', 'Houses_A', 'Bikes_A' ]
B=[ 'Cars_B', 'Planes_B', 'Houses_B', 'Bikes_B' ]
l=[deque(A),deque(B)]
n = 0
for i in l:
i.rotate(n)
n += 1
m = zip(*l)
print(list(m))
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%2f55298964%2fpython-list-of-pairs-of-elements-from-other-lists%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Using itertools.permutations
and itertools.product
with filter
:
import itertools
l = [['_'.join([i,g])for i in ['cars', 'planes', 'houses', 'bikes']] for g in 'ABCDE']
l
[['cars_A', 'planes_A', 'houses_A', 'bikes_A'],
['cars_B', 'planes_B', 'houses_B', 'bikes_B'],
['cars_C', 'planes_C', 'houses_C', 'bikes_C'],
['cars_D', 'planes_D', 'houses_D', 'bikes_D'],
['cars_E', 'planes_E', 'houses_E', 'bikes_E']]
res = []
for sub in itertools.permutations(l, 2):
res.extend(list(filter(lambda x: (x[0].split('_')[0]!=x[1].split('_')[0]), itertools.product(*sub))))
res
[('cars_A', 'planes_B'),
('cars_A', 'houses_B'),
('cars_A', 'bikes_B'),
('planes_A', 'cars_B'),
('planes_A', 'houses_B'),
('planes_A', 'bikes_B'),
('houses_A', 'cars_B'),
...
('bikes_E', 'cars_D'),
('bikes_E', 'planes_D'),
('bikes_E', 'houses_D')]
thatlambda
line is quite unpythonic, trying to squeeze a bunch of logic in to one line
– aws_apprentice
Mar 22 at 12:10
using permutations is nice one
– soheshdoshi
Mar 22 at 12:18
add a comment |
Using itertools.permutations
and itertools.product
with filter
:
import itertools
l = [['_'.join([i,g])for i in ['cars', 'planes', 'houses', 'bikes']] for g in 'ABCDE']
l
[['cars_A', 'planes_A', 'houses_A', 'bikes_A'],
['cars_B', 'planes_B', 'houses_B', 'bikes_B'],
['cars_C', 'planes_C', 'houses_C', 'bikes_C'],
['cars_D', 'planes_D', 'houses_D', 'bikes_D'],
['cars_E', 'planes_E', 'houses_E', 'bikes_E']]
res = []
for sub in itertools.permutations(l, 2):
res.extend(list(filter(lambda x: (x[0].split('_')[0]!=x[1].split('_')[0]), itertools.product(*sub))))
res
[('cars_A', 'planes_B'),
('cars_A', 'houses_B'),
('cars_A', 'bikes_B'),
('planes_A', 'cars_B'),
('planes_A', 'houses_B'),
('planes_A', 'bikes_B'),
('houses_A', 'cars_B'),
...
('bikes_E', 'cars_D'),
('bikes_E', 'planes_D'),
('bikes_E', 'houses_D')]
thatlambda
line is quite unpythonic, trying to squeeze a bunch of logic in to one line
– aws_apprentice
Mar 22 at 12:10
using permutations is nice one
– soheshdoshi
Mar 22 at 12:18
add a comment |
Using itertools.permutations
and itertools.product
with filter
:
import itertools
l = [['_'.join([i,g])for i in ['cars', 'planes', 'houses', 'bikes']] for g in 'ABCDE']
l
[['cars_A', 'planes_A', 'houses_A', 'bikes_A'],
['cars_B', 'planes_B', 'houses_B', 'bikes_B'],
['cars_C', 'planes_C', 'houses_C', 'bikes_C'],
['cars_D', 'planes_D', 'houses_D', 'bikes_D'],
['cars_E', 'planes_E', 'houses_E', 'bikes_E']]
res = []
for sub in itertools.permutations(l, 2):
res.extend(list(filter(lambda x: (x[0].split('_')[0]!=x[1].split('_')[0]), itertools.product(*sub))))
res
[('cars_A', 'planes_B'),
('cars_A', 'houses_B'),
('cars_A', 'bikes_B'),
('planes_A', 'cars_B'),
('planes_A', 'houses_B'),
('planes_A', 'bikes_B'),
('houses_A', 'cars_B'),
...
('bikes_E', 'cars_D'),
('bikes_E', 'planes_D'),
('bikes_E', 'houses_D')]
Using itertools.permutations
and itertools.product
with filter
:
import itertools
l = [['_'.join([i,g])for i in ['cars', 'planes', 'houses', 'bikes']] for g in 'ABCDE']
l
[['cars_A', 'planes_A', 'houses_A', 'bikes_A'],
['cars_B', 'planes_B', 'houses_B', 'bikes_B'],
['cars_C', 'planes_C', 'houses_C', 'bikes_C'],
['cars_D', 'planes_D', 'houses_D', 'bikes_D'],
['cars_E', 'planes_E', 'houses_E', 'bikes_E']]
res = []
for sub in itertools.permutations(l, 2):
res.extend(list(filter(lambda x: (x[0].split('_')[0]!=x[1].split('_')[0]), itertools.product(*sub))))
res
[('cars_A', 'planes_B'),
('cars_A', 'houses_B'),
('cars_A', 'bikes_B'),
('planes_A', 'cars_B'),
('planes_A', 'houses_B'),
('planes_A', 'bikes_B'),
('houses_A', 'cars_B'),
...
('bikes_E', 'cars_D'),
('bikes_E', 'planes_D'),
('bikes_E', 'houses_D')]
answered Mar 22 at 12:02
ChrisChris
4,118523
4,118523
thatlambda
line is quite unpythonic, trying to squeeze a bunch of logic in to one line
– aws_apprentice
Mar 22 at 12:10
using permutations is nice one
– soheshdoshi
Mar 22 at 12:18
add a comment |
thatlambda
line is quite unpythonic, trying to squeeze a bunch of logic in to one line
– aws_apprentice
Mar 22 at 12:10
using permutations is nice one
– soheshdoshi
Mar 22 at 12:18
that
lambda
line is quite unpythonic, trying to squeeze a bunch of logic in to one line– aws_apprentice
Mar 22 at 12:10
that
lambda
line is quite unpythonic, trying to squeeze a bunch of logic in to one line– aws_apprentice
Mar 22 at 12:10
using permutations is nice one
– soheshdoshi
Mar 22 at 12:18
using permutations is nice one
– soheshdoshi
Mar 22 at 12:18
add a comment |
Here is a simple way using itertools.combinations
, let's just make all the pairs first and then filter
afterwards.
from itertools import combinations
def filter_(tup):
x, y = tup
p1 = x.split('_')
p2 = y.split('_')
return (p1[0] != p2[0]) and (p1[1] != p2[1])
list(filter(filter_, combinations([*A, *B], 2)))
[('Cars_A', 'Planes_B'),
('Cars_A', 'Houses_B'),
('Cars_A', 'Bikes_B'),
('Planes_A', 'Cars_B'),
('Planes_A', 'Houses_B'),
('Planes_A', 'Bikes_B'),
('Houses_A', 'Cars_B'),
('Houses_A', 'Planes_B'),
('Houses_A', 'Bikes_B'),
('Bikes_A', 'Cars_B'),
('Bikes_A', 'Planes_B'),
('Bikes_A', 'Houses_B')]
list(filter(filter_, combinations([*A, *B, *C], 2)))
[('Cars_A', 'Planes_B'),
('Cars_A', 'Houses_B'),
('Cars_A', 'Bikes_B'),
('Cars_A', 'Planes_C'),
('Cars_A', 'Houses_C'),
('Cars_A', 'Bikes_C'),
('Planes_A', 'Cars_B'),
('Planes_A', 'Houses_B'),
('Planes_A', 'Bikes_B'),
('Planes_A', 'Cars_C'),
('Planes_A', 'Houses_C'),
('Planes_A', 'Bikes_C'),
('Houses_A', 'Cars_B'),
...
add a comment |
Here is a simple way using itertools.combinations
, let's just make all the pairs first and then filter
afterwards.
from itertools import combinations
def filter_(tup):
x, y = tup
p1 = x.split('_')
p2 = y.split('_')
return (p1[0] != p2[0]) and (p1[1] != p2[1])
list(filter(filter_, combinations([*A, *B], 2)))
[('Cars_A', 'Planes_B'),
('Cars_A', 'Houses_B'),
('Cars_A', 'Bikes_B'),
('Planes_A', 'Cars_B'),
('Planes_A', 'Houses_B'),
('Planes_A', 'Bikes_B'),
('Houses_A', 'Cars_B'),
('Houses_A', 'Planes_B'),
('Houses_A', 'Bikes_B'),
('Bikes_A', 'Cars_B'),
('Bikes_A', 'Planes_B'),
('Bikes_A', 'Houses_B')]
list(filter(filter_, combinations([*A, *B, *C], 2)))
[('Cars_A', 'Planes_B'),
('Cars_A', 'Houses_B'),
('Cars_A', 'Bikes_B'),
('Cars_A', 'Planes_C'),
('Cars_A', 'Houses_C'),
('Cars_A', 'Bikes_C'),
('Planes_A', 'Cars_B'),
('Planes_A', 'Houses_B'),
('Planes_A', 'Bikes_B'),
('Planes_A', 'Cars_C'),
('Planes_A', 'Houses_C'),
('Planes_A', 'Bikes_C'),
('Houses_A', 'Cars_B'),
...
add a comment |
Here is a simple way using itertools.combinations
, let's just make all the pairs first and then filter
afterwards.
from itertools import combinations
def filter_(tup):
x, y = tup
p1 = x.split('_')
p2 = y.split('_')
return (p1[0] != p2[0]) and (p1[1] != p2[1])
list(filter(filter_, combinations([*A, *B], 2)))
[('Cars_A', 'Planes_B'),
('Cars_A', 'Houses_B'),
('Cars_A', 'Bikes_B'),
('Planes_A', 'Cars_B'),
('Planes_A', 'Houses_B'),
('Planes_A', 'Bikes_B'),
('Houses_A', 'Cars_B'),
('Houses_A', 'Planes_B'),
('Houses_A', 'Bikes_B'),
('Bikes_A', 'Cars_B'),
('Bikes_A', 'Planes_B'),
('Bikes_A', 'Houses_B')]
list(filter(filter_, combinations([*A, *B, *C], 2)))
[('Cars_A', 'Planes_B'),
('Cars_A', 'Houses_B'),
('Cars_A', 'Bikes_B'),
('Cars_A', 'Planes_C'),
('Cars_A', 'Houses_C'),
('Cars_A', 'Bikes_C'),
('Planes_A', 'Cars_B'),
('Planes_A', 'Houses_B'),
('Planes_A', 'Bikes_B'),
('Planes_A', 'Cars_C'),
('Planes_A', 'Houses_C'),
('Planes_A', 'Bikes_C'),
('Houses_A', 'Cars_B'),
...
Here is a simple way using itertools.combinations
, let's just make all the pairs first and then filter
afterwards.
from itertools import combinations
def filter_(tup):
x, y = tup
p1 = x.split('_')
p2 = y.split('_')
return (p1[0] != p2[0]) and (p1[1] != p2[1])
list(filter(filter_, combinations([*A, *B], 2)))
[('Cars_A', 'Planes_B'),
('Cars_A', 'Houses_B'),
('Cars_A', 'Bikes_B'),
('Planes_A', 'Cars_B'),
('Planes_A', 'Houses_B'),
('Planes_A', 'Bikes_B'),
('Houses_A', 'Cars_B'),
('Houses_A', 'Planes_B'),
('Houses_A', 'Bikes_B'),
('Bikes_A', 'Cars_B'),
('Bikes_A', 'Planes_B'),
('Bikes_A', 'Houses_B')]
list(filter(filter_, combinations([*A, *B, *C], 2)))
[('Cars_A', 'Planes_B'),
('Cars_A', 'Houses_B'),
('Cars_A', 'Bikes_B'),
('Cars_A', 'Planes_C'),
('Cars_A', 'Houses_C'),
('Cars_A', 'Bikes_C'),
('Planes_A', 'Cars_B'),
('Planes_A', 'Houses_B'),
('Planes_A', 'Bikes_B'),
('Planes_A', 'Cars_C'),
('Planes_A', 'Houses_C'),
('Planes_A', 'Bikes_C'),
('Houses_A', 'Cars_B'),
...
answered Mar 22 at 12:03
aws_apprenticeaws_apprentice
3,9452723
3,9452723
add a comment |
add a comment |
here is a way to do it without itertools
, but using a fast data stucture from collections
module, named deque
from collections import deque
A=[ 'Cars_A', 'Planes_A', 'Houses_A', 'Bikes_A' ]
B=[ 'Cars_B', 'Planes_B', 'Houses_B', 'Bikes_B' ]
l=[deque(A),deque(B)]
n = 0
for i in l:
i.rotate(n)
n += 1
m = zip(*l)
print(list(m))
add a comment |
here is a way to do it without itertools
, but using a fast data stucture from collections
module, named deque
from collections import deque
A=[ 'Cars_A', 'Planes_A', 'Houses_A', 'Bikes_A' ]
B=[ 'Cars_B', 'Planes_B', 'Houses_B', 'Bikes_B' ]
l=[deque(A),deque(B)]
n = 0
for i in l:
i.rotate(n)
n += 1
m = zip(*l)
print(list(m))
add a comment |
here is a way to do it without itertools
, but using a fast data stucture from collections
module, named deque
from collections import deque
A=[ 'Cars_A', 'Planes_A', 'Houses_A', 'Bikes_A' ]
B=[ 'Cars_B', 'Planes_B', 'Houses_B', 'Bikes_B' ]
l=[deque(A),deque(B)]
n = 0
for i in l:
i.rotate(n)
n += 1
m = zip(*l)
print(list(m))
here is a way to do it without itertools
, but using a fast data stucture from collections
module, named deque
from collections import deque
A=[ 'Cars_A', 'Planes_A', 'Houses_A', 'Bikes_A' ]
B=[ 'Cars_B', 'Planes_B', 'Houses_B', 'Bikes_B' ]
l=[deque(A),deque(B)]
n = 0
for i in l:
i.rotate(n)
n += 1
m = zip(*l)
print(list(m))
answered Mar 22 at 13:21
Ilia GilmijarowIlia Gilmijarow
46947
46947
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%2f55298964%2fpython-list-of-pairs-of-elements-from-other-lists%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
changed, they are all lists, sorry about that.
– Qubix
Mar 22 at 11:51
You said My attempt right now is to do nested for loops for all 5 lists. Can you show us?
– Yusufsn
Mar 22 at 11:52
How do you define "similar" element? Is it "same text up to the underscore" or sth else?
– Ilia Gilmijarow
Mar 22 at 11:57
Same text up to underscore, yes.
– Qubix
Mar 22 at 12:00
You say the order inside lists is the same, but are all elements also the same (apart from obvius "_A", "_B" difference)? the example suggests so.
– Ilia Gilmijarow
Mar 22 at 12:00