Python 打印出现次数最高的素数

Python 打印出现次数最高的素数,python,Python,我正在打印素数出现次数最多的数字。但它正在打印《泰晤士报》上的数字 #checking for the highest number import collections a= [11,11,11,23,37,53,37,37,2,11,23,21,17,12,17,17,17,17,19,19,11,11,33,33] counter=collections.Counter(a) print counter.most_common(1) #checking the number that

我正在打印素数出现次数最多的数字。但它正在打印《泰晤士报》上的数字

#checking for the highest number 
import collections
a= [11,11,11,23,37,53,37,37,2,11,23,21,17,12,17,17,17,17,19,19,11,11,33,33]
counter=collections.Counter(a)
print counter.most_common(1)


#checking the number that occurs prime times


for n in a:
    times=a.count(n)
    root=times**0.5
    i=2

    while i<= root:
       if times% i != 0:
           print n
       i+=1
#检查最大数字
导入集合
a=[11,11,11,23,37,53,37,37,2,11,23,21,17,12,17,17,17,19,19,11,11,33]
计数器=集合。计数器(a)
打印计数器。最常见(1)
#检查黄金时段出现的数字
对于a中的n:
次数=a.计数(n)
根=次**0.5
i=2
而我这个怎么样:

a = [11,11,11,23,37,53,37,37,2,11,23,21,17,12,17,17,17,17,19,19,11,11,33,33]

def is_prime(n):
    from math import sqrt
    if n % 2 == 0 and n > 2:
        return False
    return all(n % i for i in range(3, int(sqrt(n)) + 1, 2))

counter = {k: a.count(k) for k in set(a)}

from operator import itemgetter
while True:
    maximum = max(counter.items(), key=itemgetter(1))
    if is_prime(maximum[1]):
        break
    else:
        counter.pop(maximum[0])
print(maximum)     # (17, 5)
print(maximum[0])  # 17
is_prime
函数来自

在这种情况下,
11
具有6个外观是最常见的,但是
6
不是素数,因此它被忽略。第二种最常见的是
17
5
外观。因为
5
是素数,
17
是答案

请注意,检查
计数
是否为素数仅在具有最大计数的元素上执行,以最小化检查。如果不是,则元素将被
pop
ed,接下来是下一个元素


对于没有模块的解决方案,您可以在中替换代码的以下部分:


你可以使用一个理解列表,使它更容易阅读

primes = [x for x in set(your_list) if all(x % y != 0 for y in range (2, x))]
max = 0
nr = -1
for x in primes:
    if primes.count(x) > max:
        max = primes.count(x)
        nr = x

你能更具体地说明你需要什么吗?你能提供任何预期输出和电流输出吗?@magicleon输入是a,输出是17。请运行该项目并查看输出以获得更好的效果。这是一个好主意,但你能帮我解决逻辑问题吗?我希望不要使用模块。你能看到这个问题吗?@HardipinderSingh还添加了一个无模块方法。
primes = [x for x in set(your_list) if all(x % y != 0 for y in range (2, x))]
max = 0
nr = -1
for x in primes:
    if primes.count(x) > max:
        max = primes.count(x)
        nr = x