reg: playing with the list

Noah Hall enalicho at gmail.com
Fri Jun 24 10:34:49 EDT 2011


On Fri, Jun 24, 2011 at 8:01 AM, kaustubh joshi <kandrjoshi at gmail.com> wrote:
> Hey all,
> I am new here and new to python too. In general new to programming .
> I was working on aproblem.
> and need some help.
> I have a list of numbers say [2,3,5,6,10,15]
> which all divide number 30.
> Now i have to reduce this list to the numbers which are prime in number.
> i.e.
> [2,3,5]
> can somebody suggest?

Well, you can use a built-in function called "filter" to create a list
containing only values that meet a certain need, for example, this
will filter out everything that is even and put it into a new list -
>>> numbers = [1,2,3,4]
>>> def is_even(n):
	return n%2==0 # returns true if even, false if not.

>>> filter(is_even,numbers)
[2, 4]


All you need to do is to create or adapt an algorithm that tests
whether a number is prime or not, and use that along with filter on
your list of numbers. Several examples exist, it's quite a popular
question.

HTH.

Noah.



More information about the Python-list mailing list