簡體   English   中英

如何獲得 n 個二進制值的所有組合,其中 1 的數量等於或大於 0 的數量?

[英]How to get all combinations of n binary values where number of 1's are equal to or more than the number of 0's?

我想找到 0 和 1 的所有可能組合的列表。 唯一的條件是 1 的數量必須大於或等於 0 的數量。 例如,對於n = 4 4,output 應該是這樣的:

[(0, 0, 1, 1), (0, 1, 0, 1), (0, 1, 1, 0), (0, 1, 1, 1), (1, 0, 0, 1), (1, 0, 1, 0), (1, 0, 1, 1), (1, 1, 0, 0), (1, 1, 0, 1), (1, 1, 1, 0), (1, 1, 1, 1)]

有沒有一種優雅的方法可以做到這一點?

您可以使用distinct_permutations

from more_itertools import distinct_permutations

def get_combos(n):
    for i in range((n+1)//2, n + 1):
        for permutation in distinct_permutations([1] * i + [0] * (n - i), n):
            yield permutation
print(list(get_combos(4)))
# [(0, 0, 1, 1), (0, 1, 0, 1), (0, 1, 1, 0), (1, 0, 0, 1), (1, 0, 1, 0), (1, 1, 0, 0), (0, 1, 1, 1), (1, 0, 1, 1), (1, 1, 0, 1), (1, 1, 1, 0)]

在這里,我們簡單地考慮每個子列表的排列:

[0, 0, 1, 1]
[0, 1, 1, 1]
[1, 1, 1, 1]

請注意,對於大nyield語句非常有用,因為您不會一次生成所有排列。

我們需要使用distinct_permutations因為你只使用 1 和 0,所以常規排列會給你重復的元素。


如果你不想安裝另一個庫,你可以使用:

from itertools import permutations

def get_combos(n):
    for i in range(n // 2 if n%2 == 0 else n//2 + 1, n):
        for permutation in permutations([1] * i + [0] * (n - i), n):
            yield permutation
print(set(get_combos(4)))
# {(0, 1, 0, 1), (0, 1, 1, 1), (1, 0, 1, 1), (1, 1, 0, 0), (1, 1, 1, 0), (0, 1, 1, 0), (1, 0, 1, 0), (1, 0, 0, 1), (1, 1, 0, 1), (0, 0, 1, 1)}

as set將消除重復的元素,代價是需要一次處理整個排列集合(即,通過調用set ,您將立即使用整個生成器,而不是根據需要從中提取元素)。

有關 distinct_permutations 的更多詳細信息

可能不清楚為什么需要這些。 考慮這個列表:

[1, 2]

默認情況下,排列會告訴您此列表的所有排列都是

(1, 2)

(2, 1)

然而, permutations不會檢查元素是什么或者它們是否重復,所以它只是像上面那樣執行交換,如果列表是

[1, 1]

你會回來的

[(1, 1), (1, 1)]

基於@Kraigolas 出色的答案,我們甚至可以通過構建一個包含n // 2零 ( 0 ) 和n個一 ( 1 ) 的初始列表來簡化整個過程。 然后,我們僅從該初始列表中獲得長度為ndistinct permutations

因此,對於n = 4 ,我們從以下列表中返回大小為 4 的不同排列: [0, 0, 1, 1, 1, 1]

from more_itertools import distinct_permutations


def get_combos(n):
    return distinct_permutations([0] * (n // 2) + [1] * n, n)
    
    
print(list(get_combos(4)))
# [(0, 0, 1, 1), (0, 1, 0, 1), (0, 1, 1, 0), (0, 1, 1, 1), (1, 0, 0, 1), (1, 0, 1, 0), (1, 0, 1, 1), (1, 1, 0, 0), (1, 1, 0, 1), (1, 1, 1, 0), (1, 1, 1, 1)]

您可以使用itertools.product和過濾器(您至少保留其中的一半,所以不會那么浪費):

from itertools import product

def get_combos(n):
    minimum = -(-n // 2)
    return [p
            for p in product([0, 1], repeat=n)
            if sum(p) >= minimum]

print(get_combos(4))

(使用p.count(1)可能更快。)

或者itertools.combinations用於選擇 n 個索引中的 k 個設置為 0:

from itertools import combinations

def get_combos(n):
    for k in range(n//2 + 1):
        for indexes in combinations(range(n), k):
            combo = [1] * n
            for i in indexes:
                combo[i] = 0
            yield tuple(combo)

print(list(get_combos(4)))

使用 n 大小集合的最大 s 個元素(其中 s <n)< div><div id="text_translate"><p> 我知道如何使用 itertools 獲得所有可能的替換組合,但我想通過使用較大集合中有限數量的元素來限制替換組合的數量。</p><p> 舉個例子,我有一套</p><p>[0,1,2]</p><p> 我想獲得帶有替換的k組合(k = 4),但使用一組[0,1,2]中最多2個不同的元素</p><p>所以可以出現在每個組合中的元素集是:</p><pre> [0,1], [1,2], [0,2].</pre><p> 在這里,我也想避免重復組合,所以在這個例子中[0,0,0,0],[1,1,1,1]或[2,2,2,2]不應該重復。</p><p> 此示例的 output:</p><pre> [0,0,0,0] [0,0,0,1] [0,0,1,1] [0,1,1,1] [1,1,1,1] [1,1,1,2] [1,1,2,2] [1,2,2,2] [2,2,2,2] [0,0,0,2] [0,0,2,2] [0,2,2,2]</pre><p> 我希望我很清楚。 </p></div></n)<>

[英]Generate k-combinations with replacement of n-size set using maximum s-number of elements of n-size set (where s<n)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

相關問題 如何獲得等於 Python 中結果數的列表的所有數學組合 給定長度為 n 的二進制數(0 或 1 或無)列表,如何確定所有可能的組合? 我正在嘗試返回缺失值超過 n 個的所有行,我想將它們放在一個新的數據框中 Python:如何有效地計算1到n個數字的二進制表示形式中“ 1”的數量? 使用 n 大小集合的最大 s 個元素(其中 s <n)< div><div id="text_translate"><p> 我知道如何使用 itertools 獲得所有可能的替換組合,但我想通過使用較大集合中有限數量的元素來限制替換組合的數量。</p><p> 舉個例子,我有一套</p><p>[0,1,2]</p><p> 我想獲得帶有替換的k組合(k = 4),但使用一組[0,1,2]中最多2個不同的元素</p><p>所以可以出現在每個組合中的元素集是:</p><pre> [0,1], [1,2], [0,2].</pre><p> 在這里,我也想避免重復組合,所以在這個例子中[0,0,0,0],[1,1,1,1]或[2,2,2,2]不應該重復。</p><p> 此示例的 output:</p><pre> [0,0,0,0] [0,0,0,1] [0,0,1,1] [0,1,1,1] [1,1,1,1] [1,1,1,2] [1,1,2,2] [1,2,2,2] [2,2,2,2] [0,0,0,2] [0,0,2,2] [0,2,2,2]</pre><p> 我希望我很清楚。 </p></div></n)<> 如何計算1到N范圍內的所有可能組合的數量? python-在二進制表示中找到與輸入數字相等的數字1 計算字典列表中鍵值大於 1 的對象數 計算列表中包含的 0 多於 1 的元素的數量 如何僅使用 numpy 查找重復次數超過 n 次的值?
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM