【问题标题】:Get all permutations without replacement?无需替换即可获得所有排列?
【发布时间】:2014-06-17 21:01:09
【问题描述】:

我想得到一个列表的所有排列,但没有任何重复,不管顺序如何。这有点难以描述,所以我举个例子。我真的很想知道这个操作的名称,因为我一直在使用它。在 python 中实现这一点的一种简单方法也会真正帮助我。谢谢!

例如

['foo', 'bar', 'la']

==>

['foo', 'bar']
['foo', 'la']
['ba', 'la']

【问题讨论】:

  • 这不是排列,这是没有重复的组合。另外,什么“替代”?
  • 好吧,我一直在寻找智慧的结晶,谢谢

标签: python computer-science permutation combinatorics


【解决方案1】:

使用itertools.combinations:

>>> import itertools
>>> list(itertools.combinations(['foo', 'bar', 'la'], 2))
[('foo', 'bar'), ('foo', 'la'), ('bar', 'la')]

【讨论】:

    猜你喜欢
    • 2017-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-21
    • 2014-07-22
    • 2016-11-19
    相关资源
    最近更新 更多