2015-02-06 56 views
-2

說我有這個列表: [1,1,2,2] 我想穿過這個所有的排列組合。 如果我打印相同的組合將被打印4次。 對於[1,1,1,3]同一個將被打印6次, 對於[1,1,1,3,3] 12.在Python中計算重複排列

一般:(a1)! (a2)! ...(an)! 是否有任何函數可以在Python中執行此操作? 如果否,你能給我一個在Python中的算法嗎?

+0

您在算法中包含了算法。你的公式定義了一個算法。 – 2015-02-06 12:07:10

+0

好吧我知道數學,但我真的不知道如何將這些知識暗示給python – 2015-02-06 12:09:10

+0

你有沒有試過在python中搜索google階乘因子?然後乘以Python的結果?我認爲它不需要頂級編碼器級別的python體驗。 – 2015-02-06 12:10:41

回答

0

您是否在尋找類似以下的東西?

import math 

def repeats(nums): 
    counts = dict() 
    result = 1 

    for n in nums: 
     if n in counts: 
      counts[n] += 1 
     else: 
      counts[n] = 1 

    for n in counts.keys(): 
     result *= math.factorial(counts[n]) 

    return result 

print repeats([1, 1, 2, 2])  # prints 4 
print repeats([1, 1, 1, 3])  # prints 6 
print repeats([1, 1, 1, 3, 3]) # prints 12