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


Groups > comp.lang.python > #8382 > unrolled thread

Re: reg: playing with the list

Started byNoah Hall <enalicho@gmail.com>
First post2011-06-24 15:34 +0100
Last post2011-06-24 15:34 +0100
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

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

#8382 — Re: reg: playing with the list

FromNoah Hall <enalicho@gmail.com>
Date2011-06-24 15:34 +0100
SubjectRe: reg: playing with the list
Message-ID<mailman.370.1308926116.1164.python-list@python.org>
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.

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web