[英]How to apply a function to each element in a list, then make a list of the outputs?
Im trying to get prime factors by factorizing a number and adding the factors to list1
, and then factorizing every number in list1
using the same method from before and adding to list2
, so any prime factor will have a list length of 2
(If the number is a square number it prints out the square root twice, but since it is not a prime, it doesn't matter).我试图通过分解一个数字并将因子添加到
list1
来获得素因子,然后使用与之前相同的方法分解list1
中的每个数字并添加到list2
,因此任何素因子的列表长度都将为2
(如果数字是一个平方数,它两次打印出平方根,但由于它不是素数,所以没关系)。
I don't know how to get it to apply my factors() function to every element in the list1
and make a list of all the factorized factors I've made.我不知道如何让它将我的 factor() function 应用于
list1
中的每个元素,并列出我制作的所有分解因子。
import math
list1 = []
list2 = []
def factors(num1):
for x in range(1, int(math.sqrt(num1) + 1)):
if num1 % x == 0:
list2.append(int(x))
list2.append(int(num1/x))
list2.sort()
print("enter a number:")
number = int(input())
for m in range(1, int(math.sqrt(number) + 1)):
if number % m == 0:
list1.append(int(m))
list1.append(int(number/m))
list1.sort()
for y in list1:
factors(y)
print(list2)
desired output if 20 was the input如果输入为 20,则需要 output
((1,1),(1,2),(1,2,2,4)(1,5),(1,2,5,10),(1,2,4,5,10,20))
You can do this using list comprehension like so.您可以像这样使用列表推导来做到这一点。 I modified the
factors
function slightly to return the list.我稍微修改了
factors
function 以返回列表。
import math
def factors(num):
fact = []
for x in range(1, int(math.sqrt(num)+1)):
if num % x == 0:
fact.append(x)
fact.append(int(num/x))
fact.sort()
return fact
print("enter a number:")
number = int(input())
list1 = factors(number)
list2 = [factors(f) for f in list1]
Result for 20: 20 的结果:
[[1, 1], [1, 2], [1, 2, 2, 4], [1, 5], [1, 2, 5, 10], [1, 2, 4, 5, 10, 20]]
In factors
function, you are appending the factors themselves to list2
but as you want nested list of factors, you should create another list and append that to list2
instead.在
factors
function 中,您将因子本身附加到list2
但由于您想要嵌套的因子列表,您应该创建另一个列表和 append 来代替list2
。 Changed code would look something like this.更改后的代码看起来像这样。
import math
list1 = []
list2 = []
def factors(num1):
factor_list = []
for x in range(1, int(math.sqrt(num1) + 1)):
if num1 % x == 0:
factor_list.append(int(x))
factor_list.append(int(num1/x))
factor_list.sort()
list2.append(factor_list)
print("enter a number:")
number = int(input())
for m in range(1, int(math.sqrt(number) + 1)):
if number % m == 0:
list1.append(int(m))
list1.append(int(number/m))
list1.sort()
for y in list1:
factors(y)
print(list2)
Also, since you've already wrote the factors
function, you can use it for factorizing the input number itself instead of writing the same code again.此外,由于您已经编写了
factors
function,您可以使用它来分解输入数字本身,而不是再次编写相同的代码。 So a better version of the same code is:所以相同代码的更好版本是:
import math
def factors(num1):
factor_list = []
for x in range(1, int(math.sqrt(num1) + 1)):
if num1 % x == 0:
factor_list.append(int(x))
factor_list.append(int(num1/x))
factor_list.sort()
return factor_list
print("enter a number:")
number = int(input())
list1 = factors(number)
list2 = [factors(x) for x in list1]
print(list2)
Following up on the comment, if you want to include only on those elements which have length 2, you can use another list comprehension for that:跟进评论,如果您只想包含长度为 2 的元素,您可以使用另一个列表推导:
list2 = [x for x in list2 if len(x)==2]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.