我有一个清单:
list = ['john','jeff','george','peter']
我想创建以下输出:
[
[('john','jeff'),('george','peter')],
[('john','george'),('jeff','peter')],
[('john','peter'),('jeff','george')],
[('george','peter'),('john','jeff')],
[('jeff','peter'),('john','george')],
[('jeff','george'),('john','peter')]
]
一般来说,我想为2对2游戏创建所有玩家组合。在一个输出行中,一个名称只能显示一次(一个玩家一次只能在一个团队中玩)。游戏允许重复播放,所以每一对元组应该重复,但顺序不同(元组的顺序不同,元组中的项目顺序不同)。
当列表包含4个以上的元素时,例如5,输出应该是这样的:
list = ['john','jeff','george','peter','simon']
[
[('john','jeff'),('george','peter')],
[('john','george'),('jeff','peter')],
[('john','george'),('jeff','simon')],
[('john','peter'),('jeff','george')],
[('john','simon'),('jeff','george')],
[('george','peter'),('john','jeff')],
[('george','simon'),('john','jeff')],
[('jeff','peter'),('john','george')],
[('jeff','george'),('john','peter')],
[('jeff','george'),('john','peter')]
...
]
所以一场比赛总共有4名球员。其他玩家只是等待,并没有参与特定游戏。
答案 0 :(得分:3)
你可以这样做:
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
是所有对的列表,res
将每对与其补码相关联。所以输出是
[[('john', 'jeff'), ('peter', 'george')],
[('john', 'peter'), ('jeff', 'george')],
[('john', 'george'), ('jeff', 'peter')],
[('jeff', 'peter'), ('john', 'george')],
[('jeff', 'george'), ('john', 'peter')],
[('peter', 'george'), ('john', 'jeff')]]
编辑:如果列表中有超过4个元素,则应该有效:
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 in pairs]
res=sum(pair_and_comp,[])
res
输出
[[('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')]]
答案 1 :(得分:0)
以下内容如何:
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 if not set(x).intersection(set(y))]
pprint(pairs)
combinations
为我们提供了所有长度为2的对(我们将其转换为list
,因此我们不会在迭代时将其耗尽)。 set(x).intersection(set(y))
会发现x
和y
之间是否存在任何共同的项目,如果情况并非如此,我们希望保留该组合。
打印:
[[('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')]]
答案 2 :(得分:0)
这应该适合你:
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 if len(set(y[0])-set(y[1]))==2 else None
answer=[remove_dup(t) for t in y if remove_dup(t) is not None]
答案:
[(('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'))]
答案 3 :(得分:-1)
尝试使用permutations()
的itertoolsimport 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',' jeff'), ('乔治',#pe;'), (' peter',' john'), (' peter',' jeff'), (' peter',' george')]