Combinations Of Two Lists In Python
Solution 1:
You can do it like that:
import itertools
l = set(['john','jeff','george','peter'])
m=list(itertools.combinations(l, 2))
res=[[i,tuple(l.symmetric_difference(i))] for i in m]
m
is a list of all the pairs, and res
associates each pair with its complement. So the output is
[[('john', 'jeff'), ('peter', 'george')],
[('john', 'peter'), ('jeff', 'george')],
[('john', 'george'), ('jeff', 'peter')],
[('jeff', 'peter'), ('john', 'george')],
[('jeff', 'george'), ('john', 'peter')],
[('peter', 'george'), ('john', 'jeff')]]
Edit: If there are more than 4 elements in the list, this should work:
import itertools
l = set(['john','jeff','george','peter','a'])
four_tuples=list(itertools.combinations(l, 4))
pairs=[(set(i),list(itertools.combinations(i, 2))) for i in four_tuples]
pair_and_comp=[[[r,tuple(el[0].symmetric_difference(r))] for r in el[1:][0]]for el inpairs]
res=sum(pair_and_comp,[])
res
The output is
[[('john', 'jeff'), ('peter', 'george')],
[('john', 'peter'), ('jeff', 'george')],
[('john', 'george'), ('jeff', 'peter')],
[('jeff', 'peter'), ('john', 'george')],
[('jeff', 'george'), ('john', 'peter')],
[('peter', 'george'), ('john', 'jeff')],
[('john', 'jeff'), ('peter', 'a')],
[('john', 'peter'), ('jeff', 'a')],
[('john', 'a'), ('jeff', 'peter')],
[('jeff', 'peter'), ('john', 'a')],
[('jeff', 'a'), ('john', 'peter')],
[('peter', 'a'), ('john', 'jeff')],
[('john', 'jeff'), ('george', 'a')],
[('john', 'george'), ('jeff', 'a')],
[('john', 'a'), ('jeff', 'george')],
[('jeff', 'george'), ('john', 'a')],
[('jeff', 'a'), ('john', 'george')],
[('george', 'a'), ('john', 'jeff')],
[('john', 'peter'), ('george', 'a')],
[('john', 'george'), ('peter', 'a')],
[('john', 'a'), ('peter', 'george')],
[('peter', 'george'), ('john', 'a')],
[('peter', 'a'), ('john', 'george')],
[('george', 'a'), ('john', 'peter')],
[('jeff', 'peter'), ('george', 'a')],
[('jeff', 'george'), ('peter', 'a')],
[('jeff', 'a'), ('peter', 'george')],
[('peter', 'george'), ('jeff', 'a')],
[('peter', 'a'), ('jeff', 'george')],
[('george', 'a'), ('jeff', 'peter')]]
Solution 2:
How about the following:
from itertools import combinations
from pprint import pprint
names = ['john', 'jeff', 'george', 'peter', 'ringo']
combos = list(combinations(names, 2))
pairs = [[x, y] for x in combos for y in combos ifnotset(x).intersection(set(y))]
pprint(pairs)
combinations
gives us all pairs of length 2 (we convert this to a list
so we don't exhaust it when iterating over it). set(x).intersection(set(y))
finds if there are any items in common between x
and y
, and we want to keep the combination if this is not the case.
This prints:
[[('john', 'jeff'), ('george', 'peter')],
[('john', 'jeff'), ('george', 'ringo')],
[('john', 'jeff'), ('peter', 'ringo')],
[('john', 'george'), ('jeff', 'peter')],
[('john', 'george'), ('jeff', 'ringo')],
[('john', 'george'), ('peter', 'ringo')],
[('john', 'peter'), ('jeff', 'george')],
[('john', 'peter'), ('jeff', 'ringo')],
[('john', 'peter'), ('george', 'ringo')],
[('john', 'ringo'), ('jeff', 'george')],
[('john', 'ringo'), ('jeff', 'peter')],
[('john', 'ringo'), ('george', 'peter')],
[('jeff', 'george'), ('john', 'peter')],
[('jeff', 'george'), ('john', 'ringo')],
[('jeff', 'george'), ('peter', 'ringo')],
[('jeff', 'peter'), ('john', 'george')],
[('jeff', 'peter'), ('john', 'ringo')],
[('jeff', 'peter'), ('george', 'ringo')],
[('jeff', 'ringo'), ('john', 'george')],
[('jeff', 'ringo'), ('john', 'peter')],
[('jeff', 'ringo'), ('george', 'peter')],
[('george', 'peter'), ('john', 'jeff')],
[('george', 'peter'), ('john', 'ringo')],
[('george', 'peter'), ('jeff', 'ringo')],
[('george', 'ringo'), ('john', 'jeff')],
[('george', 'ringo'), ('john', 'peter')],
[('george', 'ringo'), ('jeff', 'peter')],
[('peter', 'ringo'), ('john', 'jeff')],
[('peter', 'ringo'), ('john', 'george')],
[('peter', 'ringo'), ('jeff', 'george')]]
Solution 3:
This should do the trick for you:
from itertools import combinations
l = ['john','jeff','george','peter','beni']
x= list(combinations(l,2))
y=list(combinations(x,2))
remove_dup =lambda y: y iflen(set(y[0])-set(y[1]))==2elseNone
answer=[remove_dup(t) for t in y if remove_dup(t) isnotNone]
answer:
[(('john', 'jeff'), ('george', 'peter')),
(('john', 'jeff'), ('george', 'beni')),
(('john', 'jeff'), ('peter', 'beni')),
(('john', 'george'), ('jeff', 'peter')),
(('john', 'george'), ('jeff', 'beni')),
(('john', 'george'), ('peter', 'beni')),
(('john', 'peter'), ('jeff', 'george')),
(('john', 'peter'), ('jeff', 'beni')),
(('john', 'peter'), ('george', 'beni')),
(('john', 'beni'), ('jeff', 'george')),
(('john', 'beni'), ('jeff', 'peter')),
(('john', 'beni'), ('george', 'peter')),
(('jeff', 'george'), ('peter', 'beni')),
(('jeff', 'peter'), ('george', 'beni')),
(('jeff', 'beni'), ('george', 'peter'))]
Solution 4:
try itertools with permutations()
import itertools
my_list = ['john','jeff','george','peter']
paired = []
for pair in itertools.permutations(my_list, 2):
paired.append(pair)
print paired
[('john', 'jeff'), ('john', 'george'), ('john', 'peter'), ('jeff', 'john'), ('jeff', 'george'), ('jeff', 'peter'), ('george', 'john'), ('george', 'jeff'), ('george', 'peter'), ('peter', 'john'), ('peter', 'jeff'), ('peter', 'george')]
Post a Comment for "Combinations Of Two Lists In Python"