Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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_List_Math_Prime Factoring - Fatal编程技术网

Python 我如何编写一个程序来显示按最大素数因子排序的数字列表中的值? 我的名单

Python 我如何编写一个程序来显示按最大素数因子排序的数字列表中的值? 我的名单,python,list,math,prime-factoring,Python,List,Math,Prime Factoring,我想按最大素数因子的升序对这个列表进行排序 我的意见: import math maxPrime = -1 n=numbers # Print the number of 2s that divide n while n % 2 == 0: maxPrime = 2 n >>= 1 # equivalent to n /= 2 # n must be odd at this point, # thus sk

我想按最大素数因子的升序对这个列表进行排序

我的意见:

import math
maxPrime = -1
n=numbers   
# Print the number of 2s that divide n 
while n % 2 == 0: 
    maxPrime = 2
    n >>= 1     # equivalent to n /= 2 
          
    # n must be odd at this point,  
    # thus skip the even numbers and  
    # iterate only for odd integers 
for i in range(3, int(math.sqrt(n)) + 1, 2): 
    while n % i == 0: 
        maxPrime = i 
        n = n / i 
但是,这不是打印n,我收到一个错误

TypeError: unsupported operand type(s) for %: 'list' and 'int'
我的期望输出:

Sorted by largest prime factor:
[290, 1243, 1208, 1169, 865]

您的问题在顶部,错误消息非常简单:

numbers = [865, 1169, 1208, 1243, 329]
n=numbers    # n is now another reference to the list of numbers
while n % 2 == 0:
不能将模数运算符应用于列表。我怀疑你想要的是

for n in numbers:
    while n% 2 == 0:

这会让你进入下一个错误。。。节目中有好几个节目。为您解决这些问题与堆栈溢出无关。查看这个可爱的博客寻求帮助。

我认为通过列表理解,你可以做得更好

import numpy as np
numbers = [865, 1169, 1208, 1243, 329]

#create function to be used in list comprehension
def largest_prime_factor(n):
    i = 2
    while i * i <= n:
        if n % i:
            i += 1
        else:
            n //= i
    return n

#use list comprehension to get the largest common factor
largest_fact=[largest_prime_factor(x) for x in numbers]

 #sort your numbers by the largest factor
answer=[x for _,x in sorted(zip(largest_fact,numbers))]
print (answer)

Out:
[329, 1243, 1208, 1169, 865]
将numpy导入为np
数字=[865116912081243329]
#创建用于列表理解的函数
def最大素数因数(n):
i=2

而i*i Python算术运算符并没有在列表上矢量化。你需要循环。更好的方法是,编写一个返回给定数字的最大素数因子的函数,并使用它为
排序的
函数设计一个
参数。顺便说一句,我认为你的问题不值得投反对票。Python不像您希望的那样工作(也许您来自R?),但您的问题写得相当好,并且包含了相关代码。谢谢John,是的,我有R背景。我给出的答案与@John Coleman的想法基本相同。
import numpy as np
numbers = [865, 1169, 1208, 1243, 329]

#create function to be used in list comprehension
def largest_prime_factor(n):
    i = 2
    while i * i <= n:
        if n % i:
            i += 1
        else:
            n //= i
    return n

#use list comprehension to get the largest common factor
largest_fact=[largest_prime_factor(x) for x in numbers]

 #sort your numbers by the largest factor
answer=[x for _,x in sorted(zip(largest_fact,numbers))]
print (answer)

Out:
[329, 1243, 1208, 1169, 865]