编程语言
首页 > 编程语言> > python放入列表

python放入列表

作者:互联网

def problem(n):
    myList = []
    for i in range(2, n):
        if n % i == 0:
            myList.append(i)

    return myList

有了这段代码,我想知道如何得到例如12的因数才能以[[6,2],[3,4]]的形式打印出来,例如此dosnt的顺序必须相同.

解决方法:

这应该为您工作:

import math

def problem(n):
    myList = []
    for i in range(2, int(math.sqrt(n) + 1)):
        if n % i == 0:
            myList.append([i, int(n/i)])

    return myList

要获得因子对,如果i是因子,则将n除以i,然后将其除以i对.

例:

print(problem(12)) #output: [[2, 6], [3, 4]]

标签:factors,python,math
来源: https://codeday.me/bug/20191110/2015237.html