【发布时间】:2015-09-08 10:58:18
【问题描述】:
在 Python 2.7 中,我想获取列表元素的 self-cartesian product,但没有与自身配对的元素。
In[]: foo = ['a', 'b', 'c']
In[]: [x for x in itertools.something(foo)]
Out[]:
[('a', 'b'), ('a', 'c'), ('b', 'a'), ('b', 'c'), ('c', 'a'), ('c', 'b')]
目前我这样做:
[x for x in itertools.product(foo, repeat=2) if x[0] != x[1]]
但我怀疑有一个内置的方法。这是什么?
注意:itertools.combinations wouldn't give me ('a', 'b') 和 ('b', 'a')
【问题讨论】:
标签: python combinations itertools combinatorics