itertools.permutations的无序版本

Ram*_*ams 4 python

以下程序使用构造列表中的URL itertools.permutations

def url_construct_function():
    for i in range(1, len(samplelist)):
        for item in list(permutations(samplelist, i)):
Run Code Online (Sandbox Code Playgroud)
  1. 假设有在样本列表中显示3项:abc
  2. itertools.permutations 围绕各种可能的有序组合提供了很好的描述
    • 一种
    • b
    • C
    • a,b
    • a,c
    • 公元前
    • c,b

我想让程序理解这一点,a,b并且b,a是相同的。

sch*_*ggl 6

itertools.combinations可以像itertools.permutations您想要的一样工作,并且可以满足您的要求(顾名思义)

from itertools import combinations
...
    for item in list(combinations(samplelist, i)):
    ...
a
b
c
a, b
a, c
b, c
Run Code Online (Sandbox Code Playgroud)

与组合方式不同,排列顺序并不重要。所有内容都巧妙地涵盖在文档中