Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中计算重复排列_Python_Permutation - Fatal编程技术网

在Python中计算重复排列

在Python中计算重复排列,python,permutation,Python,Permutation,假设我有这个清单: [1,1,2,2] 我想浏览一下所有的排列。 如果我打印相同的排列将打印4次。 对于[1,1,1,3],同一份将打印6次, 对于[1,1,1,3,3]12 一般来说:a1!a2!。。。一 Python中有没有这样的函数? 如果没有,你能给我一个用Python实现的算法吗?你是在寻找下面这样的算法吗 import math def repeats(nums): counts = dict() result = 1 for n in nums:

假设我有这个清单: [1,1,2,2] 我想浏览一下所有的排列。 如果我打印相同的排列将打印4次。 对于[1,1,1,3],同一份将打印6次, 对于[1,1,1,3,3]12

一般来说:a1!a2!。。。一 Python中有没有这样的函数?
如果没有,你能给我一个用Python实现的算法吗?

你是在寻找下面这样的算法吗

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

你的问题中包括了算法。你的公式定义了一个算法。好吧,我懂数学,但我真的不知道如何将这些知识运用到python中。你有没有在Google上搜索python中的阶乘?那么python中的乘法结果呢?我认为这不需要顶级程序员的python经验。一个基本的python排列搜索将引导您从这里识别重复项:我需要知道列表中每个项目的重复项数量,而不必重复计算两次。。