在python中生成唯一的二进制排列

mai*_*con 12 python python-3.x

请问,我怎样才能获得所有这些二进制排列,但在Python中没有重复?

 a = list(itertools.permutations([1, 1, 0, 0]))
 for i in range(len(a)):
     print a[i]

    (1, 1, 0, 0)
    (1, 1, 0, 0)
    (1, 0, 1, 0)
    ...
Run Code Online (Sandbox Code Playgroud)

如果它大致有效将会很棒,因为我必须使用这样的30个元素列表.

Ale*_*all 11

正如@Antti在评论中所说,这相当于查找combinations输入列表的位置,这些位置确定输出中的哪些位为1.

from itertools import combinations

def binary_permutations(lst):
    for comb in combinations(range(len(lst)), lst.count(1)):
        result = [0] * len(lst)
        for i in comb:
            result[i] = 1
        yield result

for perm in binary_permutations([1, 1, 0, 0]):
    print(perm)
Run Code Online (Sandbox Code Playgroud)

输出:

[1, 1, 0, 0]
[1, 0, 1, 0]
[1, 0, 0, 1]
[0, 1, 1, 0]
[0, 1, 0, 1]
[0, 0, 1, 1]
Run Code Online (Sandbox Code Playgroud)