码迷,mamicode.com
首页 > 编程语言 > 详细

python计算列表中素数的个数

时间:2016-02-26 18:38:06      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:

 1 #!/usr/bin/env python
 2 # Gets the number of primes in the list
 3 
 4 list1 = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 29]
 5 # Get prime function .
 6 def get_num(n):
 7     count = False
 8     for x in range(2,n-1):
 9         if n % x == 0:
10             count = True
11             break
12     if not count:
13         return n
14 # Count prime
15 new_list = []
16 for i in list1:
17     num = get_num(i)
18     if num in list1:
19         new_list.append(num)
20 print(len(new_list))

 

python计算列表中素数的个数

标签:

原文地址:http://www.cnblogs.com/topicjie/p/5221251.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!