Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #8382

Re: reg: playing with the list

References <BANLkTi=Vyuc5xOoV+2vfRZPmhUT=hO0g5A@mail.gmail.com>
From Noah Hall <enalicho@gmail.com>
Date 2011-06-24 15:34 +0100
Subject Re: reg: playing with the list
Newsgroups comp.lang.python
Message-ID <mailman.370.1308926116.1164.python-list@python.org> (permalink)

Show all headers | View raw


On Fri, Jun 24, 2011 at 8:01 AM, kaustubh joshi <kandrjoshi@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.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: reg: playing with the list Noah Hall <enalicho@gmail.com> - 2011-06-24 15:34 +0100

csiph-web