我有一个函数返回数字“n”的因子列表:
def factors(n):
i = 2
factlist = []
while i <= n:
if n% i == 0:
factlist.append(i)
i = i + 1
return factlist
现在我正在尝试创建一个函数来计算因子是n的因子的次数。我有一个名为“howManyTimesDivides”的函数返回:
def howManyTimesDivides(n, d):
i = 0
while n%d==0:
n /= 2
i += 1
return i
现在,我正在尝试将这两者结合起来,但我似乎无法将howManyTimesDivides函数应用于列表“a = factors(n)”。这就是我所拥有的:
from collections import Counter
def factorCounts(n):
a = factors(n)
map(howManyTimesDivides(n,a), a)
return dict(Counter(divides))
任何见解?
答案 0 :(得分:0)
您可以使用列表理解:
def factorCounts(n):
a = factors(n)
b = [howManyTimesDivides(n, i) for i in a]
return b
print(factorCounts(20))
输出:
[2, 1, 3, 2, 1]